0
我想把這個文本「...閱讀更多」當我的標籤文本超過3行,我想阻止我的文字不會被截斷。在UILabel的特定部分添加文字「...閱讀更多」IOS
我嘗試了一些代碼,但我有不同的iPhone寬度問題,我的代碼適用於iPhone 6s,但不適用於iPhone 4s和iPhone 5s。
這裏是我的代碼:
let originalText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tristique ipsum non pretium mollis. Curabitur tristique asfhaksjf sfjhksafhdaf ahsdifhsadfh asdfhsahfoas sdofhasdhfasdf"
label.text = originalText
let labelWidth = label.frame.size.width * CGFloat(label.numberOfLines)
let str = label.text! as NSString
let words = str.componentsSeparatedByString(" ")
var newStr = "" as NSString
for word in words {
let statement = "\(newStr) \(word) ... Read more" as NSString
let size = statement.sizeWithAttributes([NSFontAttributeName: label.font])
let readMoreStatement = " ... Read more" as NSString
let sizeReadMore = readMoreStatement.sizeWithAttributes([NSFontAttributeName : label.font])
if size.width < labelWidth {
newStr = "\(newStr) \(word)"
label.text = newStr as String
} else {
newStr = newStr.stringByAppendingString("... Read More")
label.text = newStr as String
break
}
}
這裏是4S結果:
這裏是6S結果:
任何建議我如何解決這個問題他們嗎?