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

2
  • What's your actual question? You haven't asked one yet. Commented Jun 2, 2015 at 6: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. Commented Jun 2, 2015 at 6:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.