如何检测UilLabel包含尾巴" ..." IOS / Xamarin

时间:2016-05-23 07:39:39

标签: ios xamarin

我正在使用

titleLabel.Lines = 2; titleLabel.LineBreakMode = UILineBreakMode.TailTruncation;

现在长篇文章被......打破了。

现在我想知道titleLabel是否被尾部截断,包含" ..." ?对此有什么简单的建议?因为我看不到实际titleLabel.Text字段中的......字符

1 个答案:

答案 0 :(得分:1)

您的问题类似于Change the default '...' at the end of a text if the content of a UILabel doesn't fit

没有直接选项可以访问ellipsis(三点)。你需要自己做。用于计算字符串大小的代码,剪切字符串并在字符串超出视图时添加带有所需颜色的省略号。

定义NSAttributesString

let atttext = NSAttributedString(string: text!, attributes: [NSForegroundColorAttributeName: UIColor.redColor()])

计算字符串的大小

let bounds = atttext.boundingRectWithSize(label.bounds.size, options: [], context: nil)

超出视图时对字符串执行某些操作

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()])
}

有关详细信息,您可以查看我上面提到的问题的最后一个答案。