在开始换行之前,UILabel归因文本不会填满整行

时间:2018-03-08 16:22:36

标签: ios uilabel swift4 nsattributedstring

从图像中可以看出,UILabel的背景设置为黄色。在包装到下一行之前,属性文本不会使用所有空格("在"应该在第一行)。有什么办法解决吗?

enter image description here

标签的构造如下。它位于UICollectionView标题内,并通过自动布局定位

let astring = NSMutableAttributedString(string: "You asked friends, and people at ")
astring.append(NSAttributedString(string:"Pittsburgh", 
    attributes: [.font: UIFont.boldSystemFont(ofSize: 15)]))

let label = UILabel()
label.attributedText = astring

2 个答案:

答案 0 :(得分:1)

是的,奇怪的错误......

一个解决方法,虽然我没有对它进行任何测试,只是看到它适用于你的情况。

附加"无宽度空间"最后的角色:

    let astring = NSMutableAttributedString(string: "You asked friends, and people at ")
    astring.append(NSAttributedString(string:"Pittsburgh",
                                      attributes: [.font: UIFont.boldSystemFont(ofSize: 15)]))
    astring.append(NSAttributedString(string:"\u{200b}",
                                      attributes: [.font: UIFont.systemFont(ofSize: 15)]))

结果:

enter image description here

答案 1 :(得分:0)

附加"" \ u {200b}" in Attributed String in End。希望它能起作用,