简体   繁体   English

将两行属性字符串添加到UIButton的标题不起作用

[英]Add 2 lines of attributed string to title of UIButton not working

I have a UIButton . 我有一个UIButton I'm trying to get the buttons title to have 2 lines, and the second line should be smaller than the first. 我正在尝试使buttons title具有2行,第二行应小于第一行。 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. 所以唯一的是,它没有2行。

尝试这个:

button.titleLabel?.numberOfLines = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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