Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Change font size for text in UISegmentedControl?

FlyingFoxFlyingFox Posts: 19Registered Users
edited February 2012 in iPhone SDK Development
Can the font size of text items shown in the tabs of a UISegmentedControl be changed? I need to make the text slightly smaller as it is quite big by default.

I am creating my segment control like this:
CGRect frame = CGRectMake(0, 0, 200, 30);
	UISegmentedControl *returnSegmentedControl
			= [[UISegmentedControl alloc] initWithItems:
					   [NSArray arrayWithObjects:
						@"days",
						@"weeks",
						@"months",
						@"years",
						nil]];
	returnSegmentedControl.frame = frame;
	returnSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
	returnSegmentedControl.selectedSegmentIndex = 3;
Post edited by FlyingFox on
· ·

Replies

  • sddsdd Posts: 3New Users
    edited January 2009
    After a web search turned up no suitable solutions I ended up writing the following:
    void changeUISegmentFont(UIView* aView) {
    	NSString* typeName = [[aView class] className];
    	if ([typeName compare:@"UISegmentLabel" options:NSLiteralSearch] == NSOrderedSame) {
    		UILabel* label = (UILabel*)aView;
    		[label setTextAlignment:UITextAlignmentCenter];
    		[label setFont:[UIFont boldSystemFontOfSize:14]];
    	}
    	NSArray* subs = [aView subviews];
    	NSEnumerator* iter = [subs objectEnumerator];
    	UIView* subView;
    	while (subView = [iter nextObject]) {
    		changeUISegmentFont(subView);
    	}
    }
    

    Call the function with your UISegmentedControl as the parameter. You can change the two calls to customize the label to your liking (or add extra parameters to the function). I added the setTextAlignment call because when I made the font smaller the label text was then offset to the left. There may be a better way to address this but the above approach seems to work well enough.
    · ·
  • StatCoderStatCoder Posts: 221Registered Users
    edited January 2009
    I guess you didn't want to use the Bar style of UISegmentedControl. It has a much smaller font.
    · ·
  • sddsdd Posts: 3New Users
    edited January 2009
    I thought the selected/non-selected states of the bar style segmented control were too difficult to distinguish. I prefer the higher contrast difference used by the plain style. Now that I know how segmented controls are constructed I can customize them in all kinds of ways.
    · ·
  • johnqhjohnqh Posts: 812Registered Users @ @ @
    edited January 2009
    Sometimes, you can do it doesn't mean you should do it.

    If I see a segmented control with a non-standard font, I would think the app is not very professional. I brokes the standard interface of iPhone UI.

    I understand that you want to fit more text. However, you should think of some alternatives. For example, use a short word in the control, and have a label below it to give a long explanation.
    · ·
  • dkladkla Posts: 9New Users
    edited May 2010
    How about changing the font size in just one segment?
    · ·
  • mishtimishti Posts: 32Registered Users
    edited June 2011
    StatCoder wrote: »
    I guess you didn't want to use the Bar style of UISegmentedControl. It has a much smaller font.

    hiiiiiiiii

    how to increase width of UIsegmented control Bar?

    Thanxxxxxxxxx
    · ·
  • dany_devdany_dev Posts: 4,696Tutorial Authors, Registered Users @ @ @ @ @
    edited June 2011
    mishti wrote: »
    hiiiiiiiii

    how to increase width of UIsegmented control Bar?

    Thanxxxxxxxxx

    change its frame

    yourSegmentedControl.frame = CGRectMake(x,y,w,h);
    · ·
  • numero08numero08 Posts: 1New Users
    edited February 2012
    StatCoder wrote: »
    I guess you didn't want to use the Bar style of UISegmentedControl. It has a much smaller font.

    hi.

    how to change title name of UIsegmented control item?

    Thanx.
    · ·
  • HNW999HNW999 Posts: 42Registered Users
    Was searching for a solution to this problem. Well my solution is not elegant, but took less time than searching for a different answer and meets my needs. I used IB here's what I did

    in IB create a segmented controller
    create the number of segments you need
    erase the titles in each segment
    Make segmented controller the size you need
    Grab a label and drag it on top of the segmented controller.
    type in the text field the name of the segment
    Change the font size to the size you would like.
    Change the font (by clicking on the "t notepad" and in the font section change to custom for a large selection of fonts)
    "command c" to copy / "command v" to copy and paste label with same settings to the next segment and so on.

    Don't forget to hook up segmented controller to the file owner so each segment can still go where you want it to go.

    FYI -to edit all labels at the same time: If you click on 1st label and then Command click on the rest of the labels, you can group change attributes (like font type n size and adjust alignment and such)

    Hope it helps someone.


    · ·
Sign In or Register to comment.