Swift中的CGSize sizeWithAttributes [英] CGSize sizeWithAttributes in Swift
本文介绍了Swift中的CGSize sizeWithAttributes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Objective-C中,我能够使用:
In objective-C I was able to use:
CGSize stringsize =
[strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
但在Swift语言中,我没有找到任何解决方案。
But in Swift Language I didn't found any solution for this situation.
任何帮助?
推荐答案
我所做的是这样的:
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)])
swift 3
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.x
swift 2.x
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中的CGSize sizeWithAttributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文