How to change the font size of the text on a UISeg

2019-02-23 00:30发布

问题:

Following is the code for initializing my UISegmentedControl.

- (void)initializeToolButtons
{
    NSArray *buttonTitles = [NSArray arrayWithObjects:@"ANNEXET", @"HOVET", @"GLOBEN", "ALL", nil];

    toolbuttons = [[UISegmentedControl alloc] initWithItems:buttonTitles];
    toolbuttons.segmentedControlStyle = UISegmentedControlStyleBar;
    toolbuttons.tintColor = [UIColor darkGrayColor];
    toolbuttons.backgroundColor = [UIColor blackColor];     
    toolbuttons.frame = CGRectMake(0, 0, 320, 30);

    [toolbuttons addTarget:self action:@selector(toolButtonsAction) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:toolbuttons];
}

How can I reduce the font size for each item on the UISegmentedControl?

Note: toolButtons has already been declared globally.

回答1:

Here you are

http://www.iphonedevsdk.com/forum/iphone-sdk-development/5059-change-font-size-text-uisegmentedcontrol.html



回答2:

Very simple:

UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey:UITextAttributeFont];
    [segment setTitleTextAttributes:attributes forState:UIControlStateNormal];


回答3:

Consider re-designing your interface or use the "tab" style which has a smaller font. Messing with unexposed properties might get your app rejected or break your app if they change something under the hood.

For example the code sample given doesn't work. When you tap on a segment the font for that segment gets reset to its normal size. Anything unpredictable can happen or change in your app if you do things that deviate from the normal useage of these things. So if you want an app that will continue working in following OS updates stick with the standard stuff, or make your own controls with UIButtons and rectangular background images. A hack might work now, but its not to say it will in the future.