From e96b5fdeb3f36f3a3b12c1b4fa1c5529d701d073 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Fri, 26 Mar 2021 12:17:46 +0000 Subject: [PATCH] Added `attributedText` override to support attributed text properly. --- Sources/SkyFloatingLabelTextField.swift | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/SkyFloatingLabelTextField.swift b/Sources/SkyFloatingLabelTextField.swift index e7afae3..04223a3 100644 --- a/Sources/SkyFloatingLabelTextField.swift +++ b/Sources/SkyFloatingLabelTextField.swift @@ -63,6 +63,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty // MARK: Colors fileprivate var cachedTextColor: UIColor? + fileprivate var cachedAttributedText: NSAttributedString? /// A UIColor value that determines the text color of the editable text @IBInspectable @@ -76,6 +77,21 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty } } + /// An `NSAttributedString` to display as the text field content + /// + /// **NOTE:** by setting this property to a non-nil value, changes to `textColor` + /// are ignored. + @IBInspectable + override dynamic open var attributedText: NSAttributedString? { + set { + cachedAttributedText = newValue + super.attributedText = newValue + } + get { + return cachedAttributedText + } + } + /// A UIColor value that determines text color of the placeholder label @IBInspectable dynamic open var placeholderColor: UIColor = UIColor.lightGray { didSet { @@ -526,7 +542,11 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty } else if hasErrorMessage { super.textColor = textErrorColor ?? errorColor } else { - super.textColor = cachedTextColor + if let attributedText = cachedAttributedText { + super.attributedText = attributedText + } else { + super.textColor = cachedTextColor + } } }