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

2019-02-23 00:05发布

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.

3条回答
仙女界的扛把子
2楼-- · 2019-02-23 00:24

Very simple:

UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey:UITextAttributeFont];
    [segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
查看更多
Emotional °昔
4楼-- · 2019-02-23 00:39

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.

查看更多
登录 后发表回答