分段控件设置每个细分受众群的标题

其实我有一个分段控制4段。 我需要像“通知(2)”一样在每个片段中添加属性文本。 此处(2)将以不同的颜色显示,通知将以不同的颜色显示。

我有search一些第三方库,但它不适合我

感谢和问候

我们使用标签或button时使用属性文本是有限制的。 但是你可以尝试下面的方法来达到你的要求。

-(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++; } } } 

:您必须修改代码的一部分,因为它只是build议解决您的问题