1

is there an easy way, such that each item in my UISegmentedControl for instance, has multiple lines of text?

Thanks.

ps. I've also checked there's no easy way to change the height of UISegmentedControl? Say in Code? Changing the style to "Bar" does not suit me, and setFrame does not seem to work in my case too... :((

pps. This is the approach I tried as recommended by Siba but still have some issues.

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

                [label2 setText:@"text \n 10%"];

            }
        }           
    }
user1903992
  • 465
  • 2
  • 8
  • 16
  • **For Swift 3, Xcode 8** [This answer works perfect for me](https://stackoverflow.com/a/41878632/5867574) – Shan Ye Aug 31 '17 at 10:57

1 Answers1

0

1.You can try the following.

  • create a multiline UILabel
  • fill the label with N lines of text
  • convert the label into an UIImage
  • set the image as a segments content

Like one Done in This Answer : UISegmentedControl text with multiple lines?

2.You can Take An ImageView in the back of Segmented Control with Same frame And Change images as per SelectedSegmentedIndex Like below.

But Don't forget to set Alpha of SegmentedControl to 0.05. At ViewDidLoad.

segment_Control.alpha=0.05;

enter image description here

enter image description here

enter image description here

The Coding will be Like this.

- (IBAction)segmented_Changed:(id)sender {

    if (segmented_control.selectedSegmentIndex==0) {

        segment_image.image=[UIImage imageNamed:@"tab_Act1.png"];

    }else
        if (segmented_control.selectedSegmentIndex==2) {
            segment_image.image=[UIImage imageNamed:@"tab_Act3.png"];

        }
        else if (segmented_control.selectedSegmentIndex==1) {

            segment_image.image=[UIImage imageNamed:@"tab_Act2.png"];

        }
}

3.Also You Can try This :

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; 

        }
    }           
}

Source : Multiline Text On UIsegment Control

Community
  • 1
  • 1
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
  • Hi Siba. This is not working still. first problem is I still can't display two lines of text, and second problem is that, when user clicks on the UISegmentedControl it is resized. How to deal with this?? ps. here is the code I use: (I can't add the code here for some reasons, I will modify and add the code to my question so that you can see it). thanks. – user1903992 Dec 19 '12 at 07:20
  • 1
    Then the Best Method is Create 3 images (if you have 3 segments) which are different by selected index And Put those images as background of Segmented Control. And Bring the segmented contol to Top And set Alpha 0.05; See my Updated Answer With attached Images. – Siba Prasad Hota Dec 19 '12 at 10:56
  • Still you are Having issue ? – Siba Prasad Hota Dec 22 '12 at 17:35
  • Hi Siba, sorry - I will check it I had moved to doing something else in the mean while, I will let you know when I get back to this. thanks. – user1903992 Dec 24 '12 at 06:35
  • No problem . Just Have a Try on this (particularly 2nd method) And let me Know if it works. – Siba Prasad Hota Dec 24 '12 at 07:10