UILabel 属性文本在开始新行之前不会填满整行
UILabel Attributed Text doesn't fill entire line before starting a new line
从图像中可以看出,UILabel
的背景设置为黄色。属性文本在换行到下一行之前不使用所有 space("at" 应该在第一行)。有什么办法可以解决吗?
标签构造如下。它位于 UICollectionView
header 内,并由自动布局
定位
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
是的,奇怪的错误...
一个 work-around,尽管我没有对其进行任何测试,只是想看看它是否适用于您的情况。
在末尾追加一个"no-width space"字符:
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)]))
结果:
在末尾的属性字符串中附加“”\u{200b}”。希望它能起作用,
从图像中可以看出,UILabel
的背景设置为黄色。属性文本在换行到下一行之前不使用所有 space("at" 应该在第一行)。有什么办法可以解决吗?
标签构造如下。它位于 UICollectionView
header 内,并由自动布局
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
是的,奇怪的错误...
一个 work-around,尽管我没有对其进行任何测试,只是想看看它是否适用于您的情况。
在末尾追加一个"no-width space"字符:
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)]))
结果:
在末尾的属性字符串中附加“”\u{200b}”。希望它能起作用,