我在用
titleLabel.Lines = 2; titleLabel.LineBreakMode = UILineBreakMode.TailTruncation;
现在长文本最后被...打断了。
现在我想知道 titleLabel 是否被尾部截断,是否包含“...”?对此有什么简单的建议吗??因为我在实际的 titleLabel.Text 字段中看不到 ... 字符
最佳答案
您的问题类似于Change the default '...' at the end of a text if the content of a UILabel doesn't fit
没有访问省略号
(三点)的直接选项。你需要自己做。计算字符串大小的代码,剪辑字符串并在字符串超出 View 时添加带有所需颜色的省略号。
定义一个NSAttributesString
let atttext = NSAttributedString(string: text!, attributes: [NSForegroundColorAttributeName: UIColor.redColor()])
计算字符串的大小
let bounds = atttext.boundingRectWithSize(label.bounds.size, options: [], context: nil)
当字符串超出 View 时对其进行处理
if bounds.size.width > 10 {
//Do something here, like assign a new value to `attributedText` of label or change the color
label.attributedText = NSAttributedString(string: "Labelfdjkfdsjkfdsjkf...", attributes: [NSForegroundColorAttributeName: UIColor.blackColor()])
}
更多细节,你可以看看我上面提到的问题的最后一个答案。
关于ios - 如何检测 UilLabel 包含尾部 "..."IOS/Xamarin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385223/