match pattern of the label
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
65da3ee8cd
commit
b258608b77
@ -121,7 +121,7 @@ open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
open func updateView() {
|
open func updateView() {
|
||||||
updateLabel()
|
restyleText()
|
||||||
}
|
}
|
||||||
|
|
||||||
open func updateAccessibility() {
|
open func updateAccessibility() {
|
||||||
@ -152,37 +152,117 @@ open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable {
|
|||||||
return CGSize(width: adjustedWidth, height: adjustedHeight)
|
return CGSize(width: adjustedWidth, height: adjustedHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum TextSetMode {
|
||||||
|
case text
|
||||||
|
case attributedText
|
||||||
|
}
|
||||||
|
|
||||||
|
private var textSetMode: TextSetMode = .text
|
||||||
|
|
||||||
|
/// :nodoc:
|
||||||
|
override public func setTitle(_ title: String?, for state: UIControl.State) {
|
||||||
|
// 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 = .text
|
||||||
|
styleText(title, for: state)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// :nodoc:
|
||||||
|
override public func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State) {
|
||||||
|
// 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(title, for: state)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets or sets the text alignment of the button's title label.
|
||||||
|
/// Default value = `.natural`
|
||||||
|
public var textAlignment: NSTextAlignment = .natural {
|
||||||
|
didSet {
|
||||||
|
if textAlignment != oldValue {
|
||||||
|
// Text alignment can be part of our paragraph style, so we may need to
|
||||||
|
// re-style when changed
|
||||||
|
restyleText()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets or sets the line break mode of the button's title label.
|
||||||
|
/// Default value = `.byTruncatingTail`
|
||||||
|
public var lineBreakMode: NSLineBreakMode = .byTruncatingTail {
|
||||||
|
didSet {
|
||||||
|
if lineBreakMode != oldValue {
|
||||||
|
// Line break mode can be part of our paragraph style, so we may need to
|
||||||
|
// re-style when changed
|
||||||
|
restyleText()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Methods
|
// MARK: - Private Methods
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private func updateLabel() {
|
|
||||||
|
private func restyleText() {
|
||||||
|
if textSetMode == .text {
|
||||||
|
styleText(text, for: state)
|
||||||
|
} else {
|
||||||
|
styleAttributedText(currentAttributedTitle, for: state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func styleText(_ newValue: String!, for state: UIControl.State) {
|
||||||
|
defer { invalidateIntrinsicContentSize() }
|
||||||
|
guard let newValue else {
|
||||||
|
// We don't need to use attributed text
|
||||||
|
super.setAttributedTitle(nil, for: state)
|
||||||
|
super.setTitle(newValue, for: state)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
accessibilityCustomActions = []
|
||||||
|
|
||||||
|
//create the primary string
|
||||||
|
let mutableText = NSMutableAttributedString.mutableText(for: newValue,
|
||||||
|
textStyle: textStyle,
|
||||||
|
useScaledFont: useScaledFont,
|
||||||
|
textColor: textColor,
|
||||||
|
alignment: titleLabel?.textAlignment ?? .center,
|
||||||
|
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
|
||||||
|
|
||||||
|
//apply any attributes
|
||||||
|
if let attributes = textAttributes {
|
||||||
|
mutableText.apply(attributes: attributes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set attributed text to match typography
|
||||||
|
super.setAttributedTitle(mutableText, for: state)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private func styleAttributedText(_ newValue: NSAttributedString?, for state: UIControl.State) {
|
||||||
|
defer { invalidateIntrinsicContentSize() }
|
||||||
|
guard let newValue = newValue else {
|
||||||
|
// We don't need any additional styling
|
||||||
|
super.setAttributedTitle(newValue, for: state)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//clear the arrays holding actions
|
//clear the arrays holding actions
|
||||||
accessibilityCustomActions = []
|
accessibilityCustomActions = []
|
||||||
if let text, !text.isEmpty {
|
|
||||||
//create the primary string
|
|
||||||
let mutableText = NSMutableAttributedString.mutableText(for: text,
|
|
||||||
textStyle: textStyle,
|
|
||||||
useScaledFont: useScaledFont,
|
|
||||||
textColor: textColor,
|
|
||||||
alignment: titleLabel?.textAlignment ?? .center,
|
|
||||||
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
|
|
||||||
|
|
||||||
//apply any attributes
|
let mutableText = NSMutableAttributedString(attributedString: newValue)
|
||||||
if let attributes = textAttributes {
|
|
||||||
mutableText.apply(attributes: attributes)
|
|
||||||
}
|
|
||||||
|
|
||||||
//set the attributed text
|
//apply any attributes
|
||||||
setAttributedTitle(mutableText, for: .normal)
|
if let attributes = textAttributes {
|
||||||
setAttributedTitle(mutableText, for: .highlighted)
|
mutableText.apply(attributes: attributes)
|
||||||
} else {
|
|
||||||
setAttributedTitle(nil, for: .normal)
|
|
||||||
setAttributedTitle(nil, for: .highlighted)
|
|
||||||
titleLabel?.text = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Modify attributed text to match typography
|
||||||
|
super.setAttributedTitle(mutableText, for: state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: AppleGuidelinesTouchable
|
// MARK: AppleGuidelinesTouchable
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user