简体   繁体   中英

Add 2 lines of attributed string to title of UIButton not working

I have a UIButton . I'm trying to get the buttons title to have 2 lines, and the second line should be smaller than the first. Here's my code:

var myMutable = NSMutableAttributedString(string: "someText\n\(someString)", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(17)])
myMutable.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(14), range: NSRange(location: count("someText\n") - 1, length: count(someString) - 1))

self.myButton.setAttributedTitle(myMutable, forState: .Normal)

For some reason, nothing happens when this code gets executed. What am I doing wrong, and how can I fix it?

Update

I just tried adding color to it, and that part works. So the only thing is, it doesn't have 2 lines.

尝试这个:

button.titleLabel?.numberOfLines = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM