美文网首页
swift String to Size

swift String to Size

作者: 码农淏 | 来源:发表于2018-05-30 09:08 被阅读99次

Swift 4

let myString: String = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)])

Swift 3

var originalString: String = "Some text is just here..."
let myString: NSString = originalString as NSString
let size: CGSize = myString.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])

Swift 2.3

var originalString: String = "Some text is just here..."
let myString: NSString = originalString as NSString
let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])

相关文章

网友评论

      本文标题:swift String to Size

      本文链接:https://www.haomeiwen.com/subject/vlxtsftx.html