From 87b88a30551e5b2a8fbf0030a9ec40f3a86b396a Mon Sep 17 00:00:00 2001 From: Hamza Ansari Date: Fri, 20 Oct 2017 04:18:29 +0530 Subject: [PATCH] Add attributed placeholder #39 --- Example/ViewController.swift | 11 +++++++++++ .../KMPlaceholderTextView.swift | 16 ++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Example/ViewController.swift b/Example/ViewController.swift index fbf9df2..78f9464 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -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) { diff --git a/KMPlaceholderTextView/KMPlaceholderTextView.swift b/KMPlaceholderTextView/KMPlaceholderTextView.swift index 704800d..7207335 100644 --- a/KMPlaceholderTextView/KMPlaceholderTextView.swift +++ b/KMPlaceholderTextView/KMPlaceholderTextView.swift @@ -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 + } } } @@ -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