问题
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