Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:14:11+00:00 2026-06-09T22:14:11+00:00

Starts like this, which I like: But then I add a segment and this

  • 0

Starts like this, which I like:
Screenshot1.png

But then I add a segment and this happens:
Screenshot2.png
The width is set in IB, not in code.

All I need is a method to calculate width on the fly. At the end, it would do something like this:

control.width = (labelWidths + marginWidths);
// where marginWidths = (marginWidth * control.numberOfSegments)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:14:12+00:00Added an answer on June 9, 2026 at 10:14 pm

    The method in prgrmr’s answer here works fine for its intended purpose, but this is not it.

    Rather than adding unnecessary overhead with custom UILabel subviews,
    I’ve modified the example code in the above link to come up with this:

    - (void)resizeSegmentsToFitTitles:(UISegmentedControl *)control {
        CGFloat textWidth = 0; // total width of all text labels
        CGFloat marginWidth = 0; // total width of all margins
        NSUInteger nSegments = control.subviews.count;
        UIView *aSegment = [control.subviews objectAtIndex:0];
        UIFont *theFont = nil;
    
        // get font for segment title label
        for (UILabel *label in aSegment.subviews) {
            if ([label isKindOfClass:[UILabel class]]) {
                theFont = label.font;
                break;
            }
        }
    
        // calculate width of text in each segment
        for (NSUInteger i = 0; i < nSegments; i++) {
            NSString *title = [control titleForSegmentAtIndex:i];
            CGFloat width = [title sizeWithFont:theFont].width;
            CGFloat margin = 15;
    
            if (width > 200) {
                NSString *ellipsis = @"…";
                CGFloat width2 = [ellipsis sizeWithFont:theFont].width;
    
                while (width > 200-width2) {
                    title = [title substringToIndex:title.length-1];
                    width = [title sizeWithFont:theFont].width;
                }
    
                title = [title stringByAppendingString:ellipsis];
            }
    
            [control setTitle:title forSegmentAtIndex:i];
    
            textWidth += width;
            marginWidth += margin;
        }
    
        // resize segments to accomodate text size, evenly split total margin width
        for (NSUInteger i = 0; i < nSegments; i++) {
            // size for label width plus an equal share of the space
            CGFloat textWidth = [[control titleForSegmentAtIndex:i]
                                 sizeWithFont:theFont].width;
            // the control leaves a 1 pixel gap between segments if width
            // is not an integer value; roundf() fixes this
            CGFloat segWidth = roundf(textWidth + (marginWidth / nSegments));
            [control setWidth:segWidth forSegmentAtIndex:i];
        }
    
        // set control width
        [control setFrame:CGRectMake(0, 0, (textWidth + marginWidth), 30)];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that starts Tomcat and it looks like this: rem set
For example if I write Add A customer feature which might go like this:
Array starts like this: Array ( [SMART Board] => Array ( [0] => sb1
Console dogs me. Can anybody help? The sad story starts like this. My OS
I have a batch script that looks like this: Test.bat @echo off :: Starts
I'm using VB.NET for a web-application that starts some process using CreateObject, like this:
When I run rake , it always starts with a message like this: >
Typically you start a service like this Intent i = new Intent(context,MessageService.class); context.startService(i); but
I have a situation like this: start(); <Some code> end(); I want start() function
I'm writing this function which I want to print all the sublists of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.