Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 declared globally.

like image 719
vinay Avatar asked Aug 16 '10 13:08

vinay


People also ask

How do you change the font size on a storyboard?

Change Font And Size Of UILabel In Storyboard To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

Very simple:

UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey:UITextAttributeFont];
    [segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
like image 56
Sigalit Amsalem Yemini Avatar answered Nov 11 '22 00:11

Sigalit Amsalem Yemini


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.

like image 24
Martin Avatar answered Nov 11 '22 00:11

Martin