在Objective-C中我能够使用:
CGSize stringsize =
[strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
Run Code Online (Sandbox Code Playgroud)
但在Swift语言中,我没有找到任何解决方案.
任何帮助?
hol*_*lex 143
我做的是这样的:
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [.font: UIFont.systemFont(ofSize: 14)])
Run Code Online (Sandbox Code Playgroud)
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])
Run Code Online (Sandbox Code Playgroud)
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)])
Run Code Online (Sandbox Code Playgroud)
只需使用显式转换:
var stringsize = (strLocalTelefone as NSString).sizeWithAtt...
Run Code Online (Sandbox Code Playgroud)
否则你也可以桥接它:
在更高版本的Swift中不再支持桥接.
var strLocalTelefone = "some string"
var stringsize = strLocalTelefone.bridgeToObjectiveC().sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
Run Code Online (Sandbox Code Playgroud)
这个答案至少值得关注,因为它突出了两种方法之间的潜在差异.
只需一行解决方案:
yourLabel.intrinsicContentSize.width for Objective-C/Swift
这甚至可以使您的标签文本具有自定义文本间距.
| 归档时间: |
|
| 查看次数: |
40560 次 |
| 最近记录: |