3

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.

John Kakon
  • 2,531
  • 4
  • 24
  • 28

3 Answers3

6

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.

rintaro
  • 51,423
  • 14
  • 131
  • 139
1

Use the #available expression which is recommended in the Swift book.

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

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l

Fujia
  • 1,232
  • 9
  • 14
0

Checking ios version is best way

if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_0 {
    // code
} else {
    // code
}
Chetan Prajapati
  • 2,249
  • 19
  • 24
  • Chetan Prajapati, Thank you for your answer. This condition works for me with 1 correction: use NSFoundationVersionNumber_iOS_7_1 istead of NSFoundationVersionNumber_iOS_7_0. I will wait some time for another opinions and accept your answer if it will be the best. – John Kakon Sep 10 '15 at 09:37
  • Thanks for your gentleness. :) – Chetan Prajapati Sep 10 '15 at 09:39