4

Actually i have a segmented control with 4 segment. I need to add attributed text in each segment like "Notification (2)". Here (2) will be in different color and Notification will be in different color.

I have search some third party library , But it doesn't work for me

Thanks & Regards

leppie
  • 115,091
  • 17
  • 196
  • 297
  • What's your actual question? You haven't asked one yet. – rmaddy Jun 02 '15 at 06:14
  • Question is how can i make each segment look like this way : segment title is “Notification (5)” where “Notification” colour needed to be black and colour of “(5)” needed to be blue. – Abir Chatterjee Jun 02 '15 at 06:47

1 Answers1

1

There is limitation to use attributed text as we use for label or button. But you may 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 have to modified some part of code as it's just suggestion to solve your problem

Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
  • You are using a hack to change the labels. The problem with this is that if Apple changes the way segmented controls are structured the apps using this will crash. – Duck Jul 06 '17 at 16:32