Search code examples
iosautolayoutuitextfielduilabelnslayoutconstraint

How to make UILabel and UITextField font size to be relative to screen size


I have a view controller which has a text field and label and I have set height constraint for text field and label and so I feel font size is not relative to the screen I believe.

How can I make this items' font size to be relative to the screen size?


Solution

  • You can manually change UIlabel or UITextfield font in viewDidLayoutSubviews method of your ViewController or layoutSubviews method of your superView. Like this:

    CGRect screenSize = [UIScreen mainScreen].bounds;
    textField.font = [UIFont fontWithName:@"YourFont-Name"  
        size:screenSize.size.width/15.f];