diff --git a/STTextView/STTextView.podspec b/STTextView/STTextView.podspec index 25ded4e..4d9baf7 100644 --- a/STTextView/STTextView.podspec +++ b/STTextView/STTextView.podspec @@ -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" => "tsatualdypov@gmail.com" } + spec.author = { "Tamerlan Satualdypov" => "tsatualdypov@gmail.com" } 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 diff --git a/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate b/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate index 633c4f9..1986007 100644 Binary files a/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate and b/STTextView/STTextView.xcodeproj/project.xcworkspace/xcuserdata/onl1ner.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/STTextView/STTextView/STTextView.swift b/STTextView/STTextView/STTextView.swift index 6cedb51..0960d0b 100644 --- a/STTextView/STTextView/STTextView.swift +++ b/STTextView/STTextView/STTextView.swift @@ -27,73 +27,75 @@ 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 } @@ -101,44 +103,44 @@ import UIKit 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) {