ios Swift 中的 CGSize sizeWithAttributes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24141610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CGSize sizeWithAttributes in Swift
提问by Naldo Lopes
In objective-C I was able to use:
在objective-C中,我能够使用:
CGSize stringsize =
[strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
But in Swift Language I didn't found any solution for this situation.
但是在 Swift 语言中,我没有找到针对这种情况的任何解决方案。
Any Help?
任何帮助?
采纳答案by Alok
Just one line solution:
只需一行解决方案:
yourLabel.intrinsicContentSize.width
for Objective-C / Swift
yourLabel.intrinsicContentSize.width
用于 Objective-C / Swift
This will work even your label text have custom text spacing.
即使您的标签文本具有自定义文本间距,这也将起作用。
回答by holex
what I did is something like this:
我所做的是这样的:
swift 5.x
快速 5.x
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [.font: UIFont.systemFont(ofSize: 14)])
swift 4.x
快速 4.x
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])
swift 3
迅捷 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
快速 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)])
回答by Firo
Just use explicit casting:
只需使用显式转换:
var stringsize = (strLocalTelefone as NSString).sizeWithAtt...
Otherwise you can bridge it too:
Bridging is no longer supported in later versions of Swift.
否则你也可以桥接它:
更高版本的 Swift 不再支持桥接。
var strLocalTelefone = "some string"
var stringsize = strLocalTelefone.bridgeToObjectiveC().sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
This answeris worth at least looking at, as it highlights potential differences between the two approaches.
这个答案至少值得一看,因为它突出了两种方法之间的潜在差异。
回答by Baki
You can also use this piece of code, it's easier and you don't have to create new variable just to get NSString object:
你也可以使用这段代码,它更简单,你不必为了获得 NSString 对象而创建新变量:
var stringToCalculateSize:String = "My text"
var stringSize:CGSize = (stringToCalculateSize as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
回答by user2962499
On xCode 6.3, this is what you need to do now:
在 xCode 6.3 上,这是您现在需要做的:
let font:AnyObject = UIFont(name: "Helvetica Neue", size: 14.0) as! AnyObject
let name:NSObject = NSFontAttributeName as NSObject
let dict = [name:font]
let textSize: CGSize = text.sizeWithAttributes(dict)
回答by zhang dou
On xCode 8.0, this is what you need to do now: let charSize = string.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)])
在 xCode 8.0 上,这是你现在需要做的: let charSize = string.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)])