Replacement for deprecated sizeWithFont: in iOS 7?

后端 未结 20 1279
难免孤独
难免孤独 2020-11-22 08:49

In iOS 7, sizeWithFont: is now deprecated. How do I now pass in the UIFont object into the replacement method sizeWithAttributes:?

20条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 09:06

    As the @Ayush answer:

    As you can see sizeWithFont at Apple Developer site it is deprecated so we need to use sizeWithAttributes.

    Well, supposing that in 2019+ you are probably using Swift and String instead of Objective-c and NSString, here's the correct way do get the size of a String with predefined font:

    let stringSize = NSString(string: label.text!).size(withAttributes: [.font : UIFont(name: "OpenSans-Regular", size: 15)!])
    

提交回复
热议问题