ios - 如何检测 UilLabel 包含尾部 "..."IOS/Xamarin

标签 ios xamarin

我在用

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/

相关文章:

android - 如何让我的标记每秒移动一次

c# - Xamarin studio 中没有(IOS Bundle Signing)选项

xamarin - 如何在不使用代码隐藏的情况下突出显示 Xamarin.Forms 中的 ListView 所选项目?

ios - Swift multiple viewcontroller 需要在 rootviewcontroller 中用作底页

ios - Objective-C 中的访问者模式

ios - 从另一个类访问一个类变量的值 - Objective-C

ios - 让平台连续从左向右移动然后反之亦然的最佳方法是什么?

iphone - 隐藏导航栏按钮

ios - Xamarin iOS 24 小时时间选择器

java - 不朽之物真的存在吗?