开发者

How to change the font size of the text on a UISegmentedControl?

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 decl开发者_JAVA百科ared globally.


Very simple:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜