updated TextView to mimic label/buttonbase
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
3c375bea39
commit
10fdd6bcb8
@ -71,20 +71,42 @@ open class TextView: UITextView, ViewProtocol {
|
|||||||
set { }
|
set { }
|
||||||
}
|
}
|
||||||
|
|
||||||
override public var text: String! {
|
private enum TextSetMode {
|
||||||
|
case text
|
||||||
|
case attributedText
|
||||||
|
}
|
||||||
|
|
||||||
|
private var textSetMode: TextSetMode = .text
|
||||||
|
|
||||||
|
/// :nodoc:
|
||||||
|
open override var text: String! {
|
||||||
get { super.text }
|
get { super.text }
|
||||||
set {
|
set {
|
||||||
super.text = newValue
|
// When text is set, we may need to re-style it as attributedText
|
||||||
updateLabel()
|
// with the correct paragraph style to achieve the desired line height.
|
||||||
|
textSetMode = .text
|
||||||
|
styleText(newValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override public var textAlignment: NSTextAlignment {
|
/// :nodoc:
|
||||||
|
open override var attributedText: NSAttributedString? {
|
||||||
|
get { super.attributedText }
|
||||||
|
set {
|
||||||
|
// When text is set, we may need to re-style it as attributedText
|
||||||
|
// with the correct paragraph style to achieve the desired line height.
|
||||||
|
textSetMode = .attributedText
|
||||||
|
styleAttributedText(newValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// :nodoc:
|
||||||
|
open override var textAlignment: NSTextAlignment {
|
||||||
didSet {
|
didSet {
|
||||||
if textAlignment != oldValue {
|
if textAlignment != oldValue {
|
||||||
// Text alignment can be part of our paragraph style, so we may need to
|
// Text alignment can be part of our paragraph style, so we may need to
|
||||||
// re-style when changed
|
// re-style when changed
|
||||||
updateLabel()
|
restyleText()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +131,7 @@ open class TextView: UITextView, ViewProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
open func updateView() {
|
open func updateView() {
|
||||||
updateLabel()
|
restyleText()
|
||||||
}
|
}
|
||||||
|
|
||||||
open func updateAccessibility() {}
|
open func updateAccessibility() {}
|
||||||
@ -126,26 +148,59 @@ open class TextView: UITextView, ViewProtocol {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Methods
|
// MARK: - Private Methods
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private func updateLabel() {
|
private func restyleText() {
|
||||||
|
if textSetMode == .text {
|
||||||
//clear the arrays holding actions
|
styleText(text)
|
||||||
accessibilityCustomActions = []
|
|
||||||
if let text, !text.isEmpty {
|
|
||||||
//create the primary string
|
|
||||||
let mutableText = NSMutableAttributedString.mutableText(for: text,
|
|
||||||
textStyle: textStyle,
|
|
||||||
useScaledFont: useScaledFont,
|
|
||||||
textColor: textColor!,
|
|
||||||
alignment: textAlignment,
|
|
||||||
lineBreakMode: .byWordWrapping)
|
|
||||||
//apply any attributes
|
|
||||||
if let attributes = textAttributes {
|
|
||||||
mutableText.apply(attributes: attributes)
|
|
||||||
}
|
|
||||||
attributedText = mutableText
|
|
||||||
} else {
|
} else {
|
||||||
attributedText = nil
|
styleAttributedText(attributedText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func styleText(_ newValue: String!) {
|
||||||
|
defer { invalidateIntrinsicContentSize() }
|
||||||
|
guard let newValue else {
|
||||||
|
// We don't need to use attributed text
|
||||||
|
super.attributedText = nil
|
||||||
|
super.text = newValue
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accessibilityCustomActions = []
|
||||||
|
|
||||||
|
//create the primary string
|
||||||
|
let mutableText = NSMutableAttributedString.mutableText(for: newValue,
|
||||||
|
textStyle: textStyle,
|
||||||
|
useScaledFont: useScaledFont,
|
||||||
|
textColor: textColorConfiguration.getColor(self),
|
||||||
|
alignment: textAlignment,
|
||||||
|
lineBreakMode: .byWordWrapping)
|
||||||
|
|
||||||
|
applyAttributes(mutableText)
|
||||||
|
|
||||||
|
// Set attributed text to match typography
|
||||||
|
super.attributedText = mutableText
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private func styleAttributedText(_ newValue: NSAttributedString?) {
|
||||||
|
defer { invalidateIntrinsicContentSize() }
|
||||||
|
guard let newValue = newValue else {
|
||||||
|
// We don't need any additional styling
|
||||||
|
super.attributedText = newValue
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let mutableText = NSMutableAttributedString(attributedString: newValue)
|
||||||
|
|
||||||
|
applyAttributes(mutableText)
|
||||||
|
|
||||||
|
// Modify attributed text to match typography
|
||||||
|
super.attributedText = mutableText
|
||||||
|
}
|
||||||
|
|
||||||
|
private func applyAttributes(_ mutableAttributedString: NSMutableAttributedString) {
|
||||||
|
if let textAttributes {
|
||||||
|
mutableAttributedString.apply(attributes: textAttributes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user