我找到了这些代码,但似乎无效。
func evaluateStringWidth (textToEvaluate: String) -> CGFloat{
letfont = NSFont.userFontOfSize(NSFont.systemFontSize())
let attributes = NSDictionary(object: font!, forKey:NSFontAttributeName)
let sizeOfText = textToEvaluate.sizeWithAttributes((attributes as! [String : AnyObject]))
return sizeOfText.width
}
答案 0 :(得分:0)
我不明白你的意思。 所以,我通过猜测回答。
func evaluateStringWidth (textToEvaluate: String) -> CGFloat {
let maxSize = self.view.frame.size //set your view or label size
let attributesDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(14.0)] //set your font object
let textSize = NSString(string: textToEvaluate).boundingRectWithSize(maxSize, options: .UsesLineFragmentOrigin, attributes: attributesDictionary, context: nil)
return textSize.width
}