Skip to content

Commit

Permalink
Add attributed placeholder MoZhouqi#39
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaansari09 committed Oct 19, 2017
1 parent e87fcdc commit 87b88a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()


let attributes = [
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14),
NSForegroundColorAttributeName : UIColor.black,
NSBackgroundColorAttributeName: UIColor.groupTableViewBackground,
NSStrokeWidthAttributeName : 3.0,
] as [String : Any]

let placeholder = NSAttributedString(string: "NSAttributedString NSAttributedString NSAttributedString NSAttributedString \nNSAttributedString NSAttributedString NSAttributedString NSAttributedString \nNSAttributedString NSAttributedString NSAttributedString NSAttributedString \n", attributes: attributes)
placeholderTextView.attributedPlaceholder = placeholder
}

override func viewWillAppear(_ animated: Bool) {
Expand Down
16 changes: 14 additions & 2 deletions KMPlaceholderTextView/KMPlaceholderTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ open class KMPlaceholderTextView: UITextView {

private var placeholderLabelConstraints = [NSLayoutConstraint]()

open var attributedPlaceholder: NSAttributedString? {
didSet {
placeholderLabel.attributedText = attributedPlaceholder
}
}

@IBInspectable open var placeholder: String = "" {
didSet {
placeholderLabel.text = placeholder
if attributedPlaceholder == nil{
placeholderLabel.text = placeholder
}
}
}

Expand Down Expand Up @@ -103,7 +111,11 @@ open class KMPlaceholderTextView: UITextView {
placeholderLabel.font = font
placeholderLabel.textColor = placeholderColor
placeholderLabel.textAlignment = textAlignment
placeholderLabel.text = placeholder
if let attributedPlaceholder = attributedPlaceholder{
placeholderLabel.attributedText = attributedPlaceholder
}else{
placeholderLabel.text = placeholder
}
placeholderLabel.numberOfLines = 0
placeholderLabel.backgroundColor = UIColor.clear
placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
Expand Down

0 comments on commit 87b88a3

Please sign in to comment.