Skip to content

Commit

Permalink
Fixed layout and Interface Builder issues
Browse files Browse the repository at this point in the history
  • Loading branch information
onl1ner committed Aug 8, 2020
1 parent bcfdc80 commit dd33fbe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 56 deletions.
10 changes: 5 additions & 5 deletions STTextView/STTextView.podspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Pod::Spec.new do |spec|
spec.name = "STTextView"
spec.version = "1.0.1"
spec.version = "1.0.3"

spec.summary = "STTextView is a lightweight CocoaPod that adds a placeholder to the UITextView."
spec.summary = "STTextView is a light-weight CocoaPod that adds a placeholder to the UITextView."

spec.homepage = "https://github.com/onl1ner/STTextView"
spec.license = "MIT"
spec.author = { "onl1ner" => "[email protected]" }
spec.author = { "Tamerlan Satualdypov" => "[email protected]" }

spec.platform = :ios, "10.0"
spec.swift_versions = "5.0"
spec.swift_version = "5.0"

spec.source = { :git => "https://github.com/onl1ner/STTextView.git", :tag => "#{spec.version}" }
spec.source = { :git => "https://github.com/onl1ner/STTextView.git", :tag => spec.version }

spec.source_files = "STTextView/**/*.swift"
end
Binary file not shown.
104 changes: 53 additions & 51 deletions STTextView/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,118 +27,120 @@ import UIKit
/// Placeholder string that will be shown in your TextView.
@IBInspectable public var placeholder : String = "Enter your placeholder" {
didSet {
self.placeholderLabel.text = placeholder
self.placeholderTextView.text = placeholder
}
}

/// Color that would be applied to your placeholder text.
@IBInspectable public var placeholderColor : UIColor = .gray {
didSet {
self.placeholderLabel.textColor = placeholderColor
self.placeholderTextView.textColor = placeholderColor
}
}

/// If true placeholder text will hide when user starts editing.
@IBInspectable public var shouldHidePlaceholderOnEditing : Bool = false

/// Attributed placeholder to show your attributed string.
public var attributedPlaceholder : NSAttributedString? {
didSet {
self.placeholderLabel.text = nil
self.placeholderLabel.attributedText = attributedPlaceholder
if attributedPlaceholder != nil {
self.placeholderTextView.text = nil
self.placeholderTextView.attributedText = attributedPlaceholder
}
}
}

/// Inset of a content inside your TextView. Do not use UITextView.contentInset property to change the inset.
public var textInset : UIEdgeInsets {
get { return self.textContainerInset }
set {
self.textContainerInset = newValue
self.updateLayout()
// We have to check if text property is changing at runtime.
public override var text: String! {
didSet {
self.placeholderTextView.isHidden = !self.text.isEmpty
}
}

/// If true placeholder text will hide when user starts editing.
public var shouldHidePlaceholderOnEditing : Bool = false

// We have to check if text property is changing at runtime.
open override var text: String! {
public override var contentInset: UIEdgeInsets {
didSet {
self.placeholderLabel.isHidden = !self.text.isEmpty
self.placeholderTextView.contentInset = self.contentInset
}
}

lazy private var placeholderLabel : UILabel = {
let label = UILabel()
lazy private var placeholderTextView : UITextView = {
let textView = UITextView()

textView.text = self.placeholder
textView.textColor = self.placeholderColor

label.text = self.placeholder
label.textColor = self.placeholderColor
textView.font = self.font

label.font = self.font
label.textAlignment = self.textAlignment
textView.textAlignment = self.textAlignment
textView.contentInset = self.contentInset

label.numberOfLines = 0
label.backgroundColor = .clear
textView.frame = self.bounds

label.isUserInteractionEnabled = false
label.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = .clear

return label
textView.isUserInteractionEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false

return textView
}()

public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
if textView.text.isEmpty {
if shouldHidePlaceholderOnEditing {
placeholderLabel.isHidden = true
placeholderTextView.isHidden = true
}
}
return true
}

public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
placeholderLabel.isHidden = !textView.text.isEmpty
placeholderTextView.isHidden = !textView.text.isEmpty
return true
}

public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let currentContent : NSString = NSString(string: textView.text)
let newContent = currentContent.replacingCharacters(in: range, with: text)

self.placeholderLabel.isHidden = !newContent.isEmpty
self.placeholderTextView.isHidden = !newContent.isEmpty

return true
}

private func updateLayout() -> () {
self.constraints.forEach { (constraint) in
if let firstItem = constraint.firstItem as? UILabel {
if firstItem == placeholderLabel { self.removeConstraint(constraint) }
}
}
// Method is used to update the placeholderTextView whenever
// the TextView changes in Interface Builder.
private func updatePlaceholder() -> () {
placeholderTextView.text = placeholder
placeholderTextView.textColor = placeholderColor

NSLayoutConstraint.activate(
[placeholderLabel.topAnchor.constraint(equalTo: self.topAnchor,
constant: self.textContainerInset.top),
placeholderLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor,
constant: self.textContainerInset.left + self.textContainer.lineFragmentPadding),
placeholderLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor,
constant: -self.textContainerInset.right),
placeholderLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor,
constant: -self.textContainerInset.bottom)])
placeholderTextView.font = self.font
placeholderTextView.textAlignment = self.textAlignment
}

private func addPlaceholder() -> () {
self.insertSubview(placeholderLabel, at: 0)
private func initialConfiguration() -> () {
self.delegate = self

updateLayout()
self.insertSubview(placeholderTextView, at: 0)
}

private func initialConfiguration() -> () {
self.delegate = self
public override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()

addPlaceholder()
updatePlaceholder()
}

public override func layoutSubviews() {
super.layoutSubviews()

placeholderTextView.frame = self.bounds
}

public override init(frame: CGRect, textContainer: NSTextContainer?) {
// To avoid Interface Builder render and auto-layout issues
super.init(frame: frame, textContainer: textContainer)

initialConfiguration()
}

required public init?(coder aDecoder: NSCoder) {
Expand Down

0 comments on commit dd33fbe

Please sign in to comment.