在目标C中,我能够使用:
CGSize stringsize =
[strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];但在斯威夫特语言中,我没有找到任何解决这种情况的方法。
有什么帮助吗?
发布于 2018-09-17 04:52:43
只需一行解决方案:
目标yourLabel.intrinsicContentSize.width -C/ Swift
这将工作,即使您的标签文本有自定义的文本间隔。
发布于 2014-06-10 13:21:48
我所做的是这样的:
swift 5.x
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [.font: UIFont.systemFont(ofSize: 14)])swift 4.x
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])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
var originalString: String = "Some text is just here..."
let myString: NSString = originalString as NSString
let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])发布于 2015-03-26 09:49:38
您也可以使用这段代码,它更容易,而且您不必仅仅为了获得NSString对象而创建新变量:
var stringToCalculateSize:String = "My text"
var stringSize:CGSize = (stringToCalculateSize as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])https://stackoverflow.com/questions/24141610
复制相似问题