Coder221 Coder221 - 2 months ago 21
Swift Question

Set segmented control font for any width class size

I like to increase font size for iPads only so I used the following

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName as NSCopying)


and in
viewDidLoad()


segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)


but this increases fonts in both iPhone and iPad.I like to know how to increase font only in iPads

Answer

Check device model:

let model = UIDevice.currentDevice().model

if model == "iPad" {
    let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName as NSCopying)
    segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
}else{
    let attr = NSDictionary(object: UIFont(name: "HelveticaNeue", size: 16.0)!, forKey: NSFontAttributeName as NSCopying)
    segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
}

The code can be better.