ios富文本,一段文本两种字体大小,两种颜色

来源:互联网 发布:tomcat能运行php吗 编辑:程序博客网 时间:2024/06/04 18:08

UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake(20, 20,100,16)];

label.backgroundColor = [UIColorclearColor];

label.font = [UIFontsystemFontOfSize:12];

[headerView addSubview:label];


NSString *str = @"5000个";

[label setAttributedText:[selfchangeLabelWithText:str]];




//创建一个返回富文本的方法

-(NSMutableAttributedString*)changeLabelWithText:(NSString*)needText

{

    NSMutableAttributedString *attrString = [[NSMutableAttributedStringalloc]initWithString:needText];

    UIFont *font = [UIFontsystemFontOfSize:16];

    //从第一个到倒数第一个字,设置字体大小和颜色

    [attrString addAttribute:NSFontAttributeNamevalue:fontrange:NSMakeRange(0,needText.length -1)];


    [attrString addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor]range:NSMakeRange(0,needText.length -1)];


    return attrString;

}



原创粉丝点击