UIButton 标题标签的不同大小的字符串
String of different size for UIButton titlelabel
我正在尝试将按钮操作的标题设置为如下所示
按钮中的示例字符串:
let formattedString = NSMutableAttributedString()
formattedString.append(NSAttributedString(string: "Text is small" ,
attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 8.0)]))
formattedString.append(NSAttributedString(string: "Text is large"))
在 UIAlertcontroller 中动作 UIButton
.addAction(action: AABlurAlertAction(title: formattedString, style: AABlurActionStyle.modern)
但它拒绝接受 NSMutableAttributedString 传递给标题参数
抛出错误
Cannot convert value of type 'NSMutableAttributedString' to expected argument type 'String?'
您只需更改 AABlurAlertAction 中的一些代码即可 class
self.setTitle(title, for: UIControlState.normal)
将其替换为
self.setAttributedTitle(title, for: UIControlState.normal)
并替换
public init(title: String?, style: AABlurActionStyle, handler: ((AABlurAlertAction) -> Void)?) {
和
public init(title: NSAttributedString?, style: AABlurActionStyle, handler: ((AABlurAlertAction) -> Void)?) {
尝试 setAttributedTitle() 而不是 setTitle()
button.setAttributedTitle(formattedString, for: .normal)
我正在尝试将按钮操作的标题设置为如下所示
按钮中的示例字符串:
let formattedString = NSMutableAttributedString()
formattedString.append(NSAttributedString(string: "Text is small" ,
attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 8.0)]))
formattedString.append(NSAttributedString(string: "Text is large"))
在 UIAlertcontroller 中动作 UIButton
.addAction(action: AABlurAlertAction(title: formattedString, style: AABlurActionStyle.modern)
但它拒绝接受 NSMutableAttributedString 传递给标题参数
抛出错误
Cannot convert value of type 'NSMutableAttributedString' to expected argument type 'String?'
您只需更改 AABlurAlertAction 中的一些代码即可 class
self.setTitle(title, for: UIControlState.normal)
将其替换为
self.setAttributedTitle(title, for: UIControlState.normal)
并替换
public init(title: String?, style: AABlurActionStyle, handler: ((AABlurAlertAction) -> Void)?) {
和
public init(title: NSAttributedString?, style: AABlurActionStyle, handler: ((AABlurAlertAction) -> Void)?) {
尝试 setAttributedTitle() 而不是 setTitle()
button.setAttributedTitle(formattedString, for: .normal)