How to check if systemFontOfSize (fontSize: weight :) method really exists in iOS

The goal of my application deployment is iOS 7.0. I want to use the method systemFontOfSize(fontSize: weight:)on devices with iOS 8+. iOS 7 does not support this method (with a parameter weight:), and my application crashes. Especially for iOS 7, I want to install the Helvetica Light font instead of SystemFont Light.

What is the best way to test this? Do I need to check the iOS version, or can I check the method? as?

I use fast and proven

if let font = UIFont.systemFontOfSize(12, weight: UIFontWeightLight) 

or

respondsToSelector. This did not work for me.

+2
source share
3 answers

respondsToSelector works as expected:

    let font: UIFont
    if UIFont.respondsToSelector("systemFontOfSize:weight:") {
        println("YES")
        font = .systemFontOfSize(12, weight: UIFontWeightLight)
    }
    else {
        println("NO")
        font = UIFont(name: "HelveticaNeue-Light", size: 12)!
    }

HelveticaNeue Helvetica, Neue .

+5

#available, Swift.

if #available(iOS 8, *) {
    // Use iOS 8 APIs on iOS
} else {
    // Fall back to earlier iOS APIs
}

: Apple Inc. " (Swift 2 Preerelease)". . https://itun.es/us/k5SW7.l

+1

Verifying ios version is the best way

if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_0 {
    // code
} else {
    // code
}
0
source

Source: https://habr.com/ru/post/1607969/


All Articles