【问题标题】:CGSize sizeWithAttributes in SwiftSwift 中的 CGSize sizeWithAttributes
【发布时间】:2014-07-31 06:03:39
【问题描述】:

在 Objective-C 中我可以使用:

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

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

有什么帮助吗?

【问题讨论】:

  • 我找不到获取 CGSize 对象中文本大小的解决方案...
  • 我想将按钮的大小调整为它的内容文本...
  • @NaldoLopes,仅供参考,通常不会重新编辑问题以包含解决方案。问题和答案是分开设计的。通过检查答案,它会告诉人们您使用的解决方案是什么。
  • 好吧......我的坏......坦克为你的帮助!

标签: ios swift cgsize


【解决方案1】:

我所做的是这样的:

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

斯威夫特 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 如果我使用自定义字体怎么办?
  • @Developer,显然你需要使用自定义字体而不是系统字体。
  • @holex 我是 Swift 新手。我的意思是我将如何用大小编写代码行?
  • @Developer,let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 15.0)!]) 行对我来说没有问题,我不确定你被困在哪里。
  • @NavneetSharma, String in Swift 完全支持 unicode 字符,所以应该没有区别。
【解决方案2】:

只有一条线的解决方案:

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

即使您的标签文本具有自定义文本间距,这也可以工作。

【讨论】:

    【解决方案3】:

    在 xCode 8.0 上,您现在需要执行以下操作: let charSize = string.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)])

    【讨论】:

    • Swift String 类型没有这个功能。这仅适用于 NSString。
    【解决方案4】:

    只需使用显式转换:

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

    否则你也可以桥接它:
    Swift 的更高版本不再支持桥接。

    var strLocalTelefone = "some string"
    var stringsize = strLocalTelefone.bridgeToObjectiveC().sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
    

    This answer 至少值得一看,因为它突出了两种方法之间的潜在差异。

    【讨论】:

    • 桥是不必要的。 Swift Strings 无缝连接到 NSString
    • @Bill tr​​ue,但桥接允许您直接调用sizeWithAttributes,而不是创建一个带有名称的新常量(如接受的答案所示)。它的代码更少,基本上做同样的事情(因为桥接给你一个NSString)。所以我认为创建一个新的命名常量是不必要的。
    • 如何设置按钮标题标签文本来代替“一些字符串”?
    • 你可以直接访问按钮的titleLabel:self.myButton.titleLabel.text。这就是你要找的东西
    【解决方案5】:

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

    【讨论】:

    • 你为什么要做这么奇怪的选角?为什么不直接做let dict = [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 14.0)!]
    【解决方案6】:

    您也可以使用这段代码,它更简单,您不必为了获取 NSString 对象而创建新变量:

    var stringToCalculateSize:String = "My text"
    var stringSize:CGSize = (stringToCalculateSize as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多