CGSize sizeWithAttributes in Swift

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

In objective-C I was able to use:

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


        
6条回答
  •  囚心锁ツ
    2020-12-24 11:23

    You can also use this piece of code, it's easier and you don't have to create new variable just to get NSString object:

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

提交回复
热议问题