每个细分中的细分控件集属性标题 [英] Segmented Control set attributed title in each segment

查看:35
本文介绍了每个细分中的细分控件集属性标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我有4段的分段控件.我需要在每个段中添加属性文本,例如"Notification(2)".这里(2)将使用不同的颜色,而Notification将使用不同的颜色.

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

感谢&问候

推荐答案

在使用标签或按钮时,使用属性文本存在局限性.但是您可以尝试以下方法来满足您的要求.

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

这篇关于每个细分中的细分控件集属性标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆