0

I have a UISegmentControl with default style (White). I want to add text on it. But the text that i want to put on it is a long text.

I have to show the text in 2 lines of a segment. But i dont have to raise the width of the segment Because of screen width limit & no of segments.

I had tried to put a label on segment control programmatically, but my label is not displayed. Although we can put a label on segment control using XIB. but due to dynamic nature of text & segment control, I have to draw the segment control programmatically & also put the text on it.

Guidance will be appreciated.

iCreative
  • 1,499
  • 1
  • 9
  • 22

1 Answers1

0

Hi friend segment controller already have the label as the subview, so this code is helpful to achieve the multiline text to segment control

for (id segment in [segmentedControl subviews]) 
{
    for (id label in [segment subviews]) 
    {
        if ([label isKindOfClass:[UILabel class]])
        {
            //hear u add any of delegate function to increase the height and other label functionality in this 
            [label setTextAlignment:UITextAlignmentCenter];
            [label setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
  CGSize labelSize = CGSizeMake(100, 80);
  CGSize theStringSize = [label.text sizeWithFont:label.font constrainedToSize:labelSize];
  CGRect frame = label.frame;
  frame.size = theStringSize; 

        }
    }           
}

Have a Good day

Balakrishnan Mca
  • 747
  • 5
  • 11
  • 3
    Warning: this code might break in future versions of iOS as it's using undocumented properties of UISegmentedControl. – chrisben Mar 27 '13 at 14:10
  • I am not agree with your comment. Most(50-60)% of the app using undocumented properties of varios controls for custom UI in iOS. – Balakrishnan Mca May 17 '13 at 09:14
  • In the apps I develop I try not to do such things.If you use undocumented properties your app might stop working or have a strange behaviour when run on a new version of iOS: what is not documented is also not supported! – chrisben May 17 '13 at 20:07
  • I would have to agree this maybe uncertain moving forward. The more accepted solution would be to turn them into images. – Ace Green Jul 02 '16 at 19:56