Advertise here




Advertise here

Howdy, Stranger!

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

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Change font size for text in UISegmentedControl?

FlyingFoxFlyingFox Posts: 19Registered Users
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
    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
    I guess you didn't want to use the Bar style of UISegmentedControl. It has a much smaller font.
    STAT ICD-9 LITE|<a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291155883&mt=8" target="_blank
  • sddsdd Posts: 3New Users
    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
    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
    How about changing the font size in just one segment?
  • mishtimishti Posts: 32Registered Users
    StatCoder;40031 said:
    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,700Tutorial Authors, Registered Users
    mishti;341298 said:
    hiiiiiiiii

    how to increase width of UIsegmented control Bar?

    Thanxxxxxxxxx
    change its frame

    yourSegmentedControl.frame = CGRectMake(x,y,w,h);
  • numero08numero08 Posts: 1New Users
    StatCoder;40031 said:
    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: 40Registered 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.