Segmented control assigned a name in each segment
Actually I have a segmented control with 4 segments. I need to add attributed text in each segment like "Notice (2)". Here (2) will be different colors and the notification will have different colors.
I have a regular third party library but it doesn't work for me
Thanks and Regards
+3
source to share
1 answer
There is a limitation to using the attributed text that we use for the label or button. But you can try below method to achieve your requirements.
-(void)SetSegmentValue:(NSString *)value forSegment:(int)index RangeOfBlueColor:(NSRange)bluecolorRange{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:value];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[paragrahStyle setLineBreakMode:NSLineBreakByWordWrapping];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, value.length)];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, value.length)];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:bluecolorRange];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0] range:NSMakeRange(0, value.length)];
int i =0 ;
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) {
if ([v isKindOfClass:[UILabel class]]&& i== index) {
UILabel *label=(UILabel *)v ;
[label setAttributedText:attributedString];
i++;
}
}
}
NOTE . You should change some of the code as this is just a suggestion to solve your problem.
+1
source to share