Search code examples
iosxcodeswiftios7system-font

How to check does method systemFontOfSize(fontSize: weight:) exist in iOS


My application deployment target is iOS 7.0 I want to use method systemFontOfSize(fontSize: weight:) on devices with iOS 8+. iOS 7 doesn't support this method (with weight: parameter) and my application crashes. Specially for iOS 7 I want to set Helvetica Light font instead of SystemFont Light.

What is the best way to check it? Do I need to check iOS version or I can check the method? how?

I use swift and tried

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

or

respondsToSelector method. It didn't work for me.


Solution

  • 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)!
        }
    

    And I would recommend HelveticaNeue instead of Helvetica, since the system font is Neue one.