Skip to content

Commit

Permalink
Added support for displaying placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaguilar committed Oct 11, 2017
1 parent dfd9a29 commit d6806ad
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Example/MultilineTextField/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<constraints>
<constraint firstAttribute="height" constant="128" id="EXo-qg-uwL"/>
</constraints>
<string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
<string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </string>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
Expand Down
11 changes: 11 additions & 0 deletions Example/MultilineTextField/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class ViewController: UIViewController {
// imageView.bounds = CGRect(x: 0, y: 0, width: 50, height: 60)
// imageView.backgroundColor = .red
textField.leftView = imageView
textField.placeholder = "This is my placeholder"

print("Printing \(textField.subviews.count) views")

textField.subviews.forEach { v in
print("********")
print(v)
}

print("********")

}

override func didReceiveMemoryWarning() {
Expand Down
67 changes: 66 additions & 1 deletion MultilineTextField/Classes/MultilineTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,37 @@
import Foundation
import UIKit

public class MultilineTextField: UITextView {
public class MultilineTextField: UITextView, UITextViewDelegate {
private let placeholderView: UITextView

public override var text: String! {
didSet {
self.textViewDidChange(self)
}
}

public override var attributedText: NSAttributedString! {
didSet {
self.textViewDidChange(self)
}
}


/// The string that is displayed when there is no other text in the text field.
/// This value is nil by default. The placeholder string is drawn using the color
/// stored in `self.placeholderColor`.
public var placeholder: String? {
didSet {
placeholderView.text = placeholder
}
}

public var placeholderColor: UIColor = .black {
didSet {
placeholderView.textColor = placeholderColor
}
}

public var leftView: UIView? {
willSet {
if let view = self.leftView {
Expand All @@ -25,7 +55,42 @@ public class MultilineTextField: UITextView {
let exclusionPath = UIBezierPath(rect: view.frame)

self.textContainer.exclusionPaths = [exclusionPath]
self.placeholderView.textContainer.exclusionPaths = self.textContainer.exclusionPaths
}
}
}

public override init(frame: CGRect, textContainer: NSTextContainer?) {
placeholderView = UITextView(frame: frame, textContainer: textContainer)
super.init(frame: frame, textContainer: textContainer)
initializeUI()
}

public required init?(coder aDecoder: NSCoder) {
// FIXME: What happens with the stored placeholder?
placeholderView = UITextView(coder: aDecoder) ?? UITextView()
super.init(coder: aDecoder)
initializeUI()

}

func initializeUI() {
self.delegate = self

self.insertSubview(placeholderView, at: 0)

placeholderView.frame = self.bounds
placeholderView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

placeholderView.text = ""
placeholderView.isEditable = false
placeholderView.textColor = UIColor(white: 0.7, alpha: 1)

textViewDidChange(self)
}

public func textViewDidChange(_ textView: UITextView) {
placeholderView.isHidden = !textView.text.isEmpty
|| !textView.attributedText.string.isEmpty
}
}

0 comments on commit d6806ad

Please sign in to comment.