I need to set multiple lines of text in each segment UISegmentedControl. I tried the following code and it works fine, but the problem is that when the page loads for the first time and the segmented control is displayed for the first time, it does not display several lines of text, but when I click on one of the segments, the text appears in several lines. How to fix it? Here is the code:
-(void)configureInitialPage
{
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:trends];
segmentedControl.tintColor = [UIColor colorWithRed:0.03 green:0.28 blue:0.51 alpha:1.0];
for (id segment in [segmentedControl subviews]) {
for (id label in [segment subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *) label;
titleLabel.frame = CGRectMake(0, 0, 97, 50);
[titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.numberOfLines = 0;
}
}
}
[segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(20, 510, 720, 40);
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl layoutIfNeeded];
[self.view addSubview:segmentedControl];
}
source
share