ASButtonNode attributedTitle用于不同的controlStates

时间:2019-06-04 07:16:05

标签: ios swift asyncdisplaykit

ASButtonNodeattributedTitle对于不同的控制状态和使用小数大小有问题。标题变为左侧。

如果我为按钮使用磅值,则我的行为正确。

我正在运行texture/asyncDisplayKit 2.8.1

/* Set title */
testButton.setAttributedTitle(NSAttributedString(string: "NORMAL"), for: .normal)
testButton.setAttributedTitle(NSAttributedString(string: "HIGHLIGHTED"), for: .highlighted)

/* styling */
/** This works
 * testButton.style.width = ASDimensionMake("120pt") 
 **/
testButton.style.width = ASDimensionMake("30%")
testButton.style.height = ASDimensionMake(120)

点击按钮后,标题不应向右移动。我想念什么吗?

突出显示状态后

enter image description here

突出显示状态之前

enter image description here

1 个答案:

答案 0 :(得分:0)

也许您可以在alignment上指定paragraphStyle

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attributes = [ ... your current style,
                  .paragraphStyle: paragraphStyle] as [NSAttributedString.Key: Any]
]

let attributedString = NSAttributedString(string: "your text here", attributes: attributes)

您可以为每个州创建2个attributedString,而不是分别为每个州设置

testButton.setAttributedTitle(normalAttributedString, for: .normal)
testButton.setAttributedTitle(highlightedAttributedString, for: .highlight)

快乐纹理