首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >斯威夫特中的CGSize sizeWithAttributes

斯威夫特中的CGSize sizeWithAttributes
EN

Stack Overflow用户
提问于 2014-06-10 12:57:49
回答 5查看 46.3K关注 0票数 53

在目标C中,我能够使用:

代码语言:javascript
运行
复制
    CGSize stringsize =
     [strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];

但在斯威夫特语言中,我没有找到任何解决这种情况的方法。

有什么帮助吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-09-17 04:52:43

只需一行解决方案:

目标yourLabel.intrinsicContentSize.width -C/ Swift

这将工作,即使您的标签文本有自定义的文本间隔。

票数 7
EN

Stack Overflow用户

发布于 2014-06-10 13:21:48

我所做的是这样的:

swift 5.x

代码语言:javascript
运行
复制
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [.font: UIFont.systemFont(ofSize: 14)])

swift 4.x

代码语言:javascript
运行
复制
let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])

swift 3

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
var originalString: String = "Some text is just here..."
let myString: NSString = originalString as NSString
let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
票数 159
EN

Stack Overflow用户

发布于 2015-03-26 09:49:38

您也可以使用这段代码,它更容易,而且您不必仅仅为了获得NSString对象而创建新变量:

代码语言:javascript
运行
复制
var stringToCalculateSize:String = "My text"
var stringSize:CGSize = (stringToCalculateSize as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24141610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档