IvanovDeveloper IvanovDeveloper - 3 months ago 17
iOS Question

Which of the following will set the font of UISegmentedControl?


  • UIFont font = [UIFont boldSystemFontOfSize: 12.0f];
    NSDictionary attributes = [NSDictionary dictionaryWithObject: font forKey: UITextAttributeFont];
    [segmentedControl setTitleTextAttributes: attributes forState: UIControlStateNormal];

  • [[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

  • segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);

  • None of these.


Answer
UIFont font = [UIFont boldSystemFontOfSize: 12.0f];
NSDictionary attributes = [NSDictionary dictionaryWithObject: font forKey: UITextAttributeFont];
[segmentedControl setTitleTextAttributes: attributes forState: UIControlStateNormal];

The above code will change font for one particular segmentcontrol whereas the below code will change the font for all segmentcontrol in your app.

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

and this code segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f); will just scale down your segmentcontrol

Note:

Just replace deprecated UITextAttributeFont with NSFontAttributeName to work without warning.

Comments