将 sizeWithFont 方法转换为 Swift

Kah*_*hsn 4 objective-c ios swift

我正在阅读旧的 obj-c 教程并且很难理解 obj-c 代码。而且它们显然已被弃用。我已经研究了其他 stackoverflow 问题,但我仍然无法弄清楚,因为它们都不是在 Swift 中的。

   CGSize textSize = [text sizeWithFont:textFont constrainedToSize:CGSizeMake(printableFrame.size.width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
   CGRect textFrame = CGRectMake(printableFrame.origin.x, currentY, textSize.width, textSize.height);
Run Code Online (Sandbox Code Playgroud)

如何将上面的代码转换为 Swift?

Xma*_*hts 6

该方法已被弃用,取而代之的是 sizeWithAttributes。

替换已弃用的 sizeWithFont: 在 iOS 7 中?

要转换为 swift,不幸的是,您似乎必须在代码中使用 NSString

Swift 中的 CGSize sizeWithAttributes

干净,但这会影响性能,因此如果这些行被调用很多,您可能需要找到另一种解决方案。