Multiline Text On UIsegment Control

余生颓废 提交于 2019-12-11 16:48:17

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/8784401/multiline-text-on-uisegment-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!