在lineBreakingMode
设置为ByTruncatingHead
或ByTruncatingTail
的标签中是否有一种摆脱省略号(...)的Swift方式?
我遇到Obj-C方式试图将其转换为快速但无效(我失败了)。 非常感谢任何帮助或提示。
这是我的转换:
private func replaceElipses() {
let origSize = self.frame
let useWidth = origSize.size.width
self.sizeToFit()
let labelSize = (self.text! as NSString).sizeWithAttributes([NSFontAttributeName: self.font])
if labelSize.width > useWidth {
original = self.text!
truncateWidth = useWidth
systemfont = self.font
subLength = (self.text?.characters.count)!
var temp = ((self.text)! as NSString).substringToIndex(self.text!.characters.count-1) as NSString
temp = temp.substringToIndex(self.getTruncatedStringPoint(subLength))
temp = "\(temp)\("@")"
var count = 0
while (self.text! as NSString).sizeWithAttributes([NSFontAttributeName: self.font]).width > useWidth {
count++
temp = ((self.text)! as NSString).substringToIndex(self.text!.characters.count-(1+count)) as NSString
temp = "\(temp)\("@")"
}
self.text = temp as String
self.frame = origSize
} else {
self.frame = origSize
}
}
func getTruncatedStringPoint(splitPoint: Int) -> Int {
let splitLeft = original.substringToIndex(splitPoint) as NSString
subLength /= 2
if subLength <= 0 {
return splitPoint
} else if splitLeft.sizeWithAttributes([NSFontAttributeName: systemfont]).width > truncateWidth {
return self.getTruncatedStringPoint(splitPoint - subLength)
} else if splitLeft.sizeWithAttributes([NSFontAttributeName: systemfont]).width < truncateWidth {
return self.getTruncatedStringPoint(splitPoint + subLength)
} else {
return splitPoint
}
}