多行NSAttributed String作为UIButton标题

DMo*_*Mop 3 string uibutton nsattributedstring ios swift

我的问题在理论上与此类似:UIButton上的iOS NSAttributedString

我希望将我的按钮的标题读为:

"带下划线的字符串

一些文字"

这需要以swift和100%以编程方式完成.

我试图通过使用NSMutableAttributedString创建带下划线的部分,然后将其附加到其他文本(使用换行符引导)来尝试这样做.但是,这给了我错误"无法分配类型'Void'('aka'()')的值来键入'NSMutableAttributedString"

代码如下:

var patientName = NSMutableAttributedString(string:"Patient Name", attributes: underlineAttributes)
var clickforinfomessage = NSMutableAttributedString(string: "\nclick for patient info")
clickforinfomessage = clickforinfomessage.appendAttributedString(patientName)
startVisitButton.setAttributedTitle(clickforinfomessage, forState: .Normal)
Run Code Online (Sandbox Code Playgroud)

Dha*_*esh 13

你可以这样做:

let dict1 = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]

let attString = NSMutableAttributedString()
attString.appendAttributedString(NSAttributedString(string: "Patient Name\n", attributes: dict1))
attString.appendAttributedString(NSAttributedString(string: "click for patient info", attributes: nil))
startVisitButton.setAttributedTitle(attString, forState: .Normal)
startVisitButton.titleLabel?.numberOfLines = 0
startVisitButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
Run Code Online (Sandbox Code Playgroud)

结果将是:

在此输入图像描述