Different font sizes for uibutton and uilabel for

2019-06-08 18:30发布

问题:

I am working on a quiz game,it started as an iphone app,now it is fully working and i want to make it an universal app,but i can't find a way to set the font size to one value for all iphones and another for the ipad

I tried with the auto shrink menu in the interface builder,but it works only for labels,and if i do the same via code for uibutton the code doesn't work,this is the code i wrote inside viewdidload

ans2b.titleLabel?.numberOfLines = 1
ans2b.titleLabel?.adjustsFontSizeToFitWidth = true
ans2b.titleLabel?.minimumScaleFactor = 2

For the label i used interface builder and it works but the labels changes the font size every time the text inside it changes

How can i put a value for the font size for all iphones and a value for all ipads?Can i do it with interface builder or via code? Maybe can i put a value in interface builder and some condition in viewdidload to change the size if the device is an ipad

Thanks

回答1:

You can always check for device type.

if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        //label font size for iPad
    }
    else
    {
        //label font size for iPhones
    }

We have a enum to define for device type check.

    enum UIUserInterfaceIdiom : Int {
    case Unspecified
    case Phone // iPhone and iPod touch style UI
    case Pad // iPad style UI
}