Swift中的CGSize sizeWithAttributes

53

在Objective-C中,我能够使用:

    CGSize stringsize =
     [strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];

但是在Swift语言中,我没有找到任何解决这种情况的方法。

可以帮忙吗?


3
我无法找到获取 CGSize 对象中文本大小的解决方案。 - Naldo Lopes
我想将按钮调整到其内容文本的大小... - Naldo Lopes
@NaldoLopes,提醒一下,通常问题不会被重新编辑以包含解决方案。问题和答案是分开设计的。通过勾选答案,可以告诉人们你使用的解决方案是什么。 - Firo
好的...我的错...谢谢你的帮助! - Naldo Lopes
6个回答

160

我所做的类似于这样:

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)])

@开发者,显然你需要使用自定义字体而不是系统字体。 - holex
@holex,我是Swift的新手。我的意思是,我该如何编写带有大小的代码行? - Developer
2
@开发者,这行代码 let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 15.0)!]) 对我来说没有问题,我不确定你卡在哪里了。 - holex
1
@NavneetSharma,Swift中的String完全支持Unicode字符,因此没有任何区别。 - holex
不需要使用NSAttributedStringKey.font,可以写成.font。 它被推断为NSAttributedStringKey ==> size(withAttributes: [.font : UIFont(...)]) - J. V.
显示剩余10条评论

8

只需使用显式转换:

var stringsize = (strLocalTelefone as NSString).sizeWithAtt...

否则,您也可以进行桥接:
在较新版本的Swift中,不再支持桥接。
var strLocalTelefone = "some string"
var stringsize = strLocalTelefone.bridgeToObjectiveC().sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])

这篇答案值得一看,因为它突出了两种方法之间的潜在差异。


1
桥接是不必要的。Swift String 无缝桥接到 NSString - Bill
@Bill 是的,但桥接允许您直接调用 sizeWithAttributes,而不是创建一个带名称的新常量(如所接受的答案中所示)。这样可以减少代码量,并且基本上做相同的事情(因为桥接会给您一个 NSString)。因此,我认为创建一个新的命名常量是不必要的。 - Firo
如何将按钮的titleLabel文本设置为“某个字符串”? - Toseef Khilji
你可以直接访问按钮的 titleLabelself.myButton.titleLabel.text。这是你要找的吗? - Firo

7

仅需一行代码:

yourLabel.intrinsicContentSize.width 对于Objective-C / Swift

即使您的标签文本具有自定义文本间距,此方法也适用。


2
您也可以使用这段代码,它更容易,您不必创建新变量来获取NSString对象:
var stringToCalculateSize:String = "My text"
var stringSize:CGSize = (stringToCalculateSize as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])

0

在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)

1
你为什么要做那些奇怪的转换?干嘛不直接用let dict = [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 14.0)!]呢? - Aaron Brager

-3
在xCode 8.0中,现在需要做的是: let charSize = string.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)])

Swift的String类型没有这个函数,只有NSString才有。 - Mihai Georgescu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接