4

I am working on an iOS app and I observed the UI in iPhone 5,6,6+ all fonts are different device by device, but my requirement is button sizes and font sizes must be same for iPhone4s,5 and different for iPhone 6 and 6+.How can I achieve this in my app. I know we can do by programmatically, but is there any chance to do on storyboard using adaptive layouts.

I am using xcode7.2, swift2.

Thanks in advance..

Krish Allamraju
  • 703
  • 1
  • 7
  • 29
Ramakrishna
  • 712
  • 8
  • 26
  • 1
    Have you checked this https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/ChangingtheFontforaSizeClass.html – Zell B. Jan 11 '16 at 14:55
  • I don't understand 'all fonts are different device by device' as the standard system font is identical? – FractalDoctor Jan 24 '16 at 20:14

1 Answers1

0

I was facing the same problem and solved it using Swifts Computed Properties. I created a static variable fontsize which gets dynamically initialized with the appropriate fontsize due to the size of the screen.

import UIKit

class ViewFunctions {

    let screenSize = UIScreen.mainScreen().bounds.size
    static var fontsize: CGFloat {
        get {
            if screenSize.height >= 1024 { // iPad Pro
                return 16.0
            } else if screenSize.height >= 768 { // iPad
                return 16.0
            } else if screenSize.height >= 414 { // iPhone 6Plus
                return 15.0
            } else if screenSize.height >= 375 { // iPhone 6/s
                return 15.0
            } else if screenSize.height >= 320 { // iPhone 5/s
                return 14.0
            } else if screenSize.height >= 319 { // iPhone 4/s
                return 14.0
            } else {
                return 14.0
            }
        }
    }

}

Then use it for example to set the fontsize of a button-label:

import UIKit

class TestClass {    

    var testButton: UIButton = UIButton(type: UIButtonType.System) as UIButton!
    testButton.titleLabel!.font = UIFont(name: "Helvetica Neue", size: ViewFunctions.fontsize)
    testButton.setTitle("Test", forState: .Normal)
    // add button to view or sth like that...

}
snbl
  • 76
  • 3