CGSize sizeWithAttributes in Swift

前端 未结 6 2224
孤独总比滥情好
孤独总比滥情好 2020-12-24 10:50

In objective-C I was able to use:

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


        
6条回答
  •  萌比男神i
    2020-12-24 11:18

    what I did is something like this:

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

提交回复
热议问题