Search code examples
iosswiftuibuttonnsmutableattributedstring

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.


Solution

  • Try this:

    button.titleLabel?.numberOfLines = 0;