博客> 利用NSMutableAttributedString实现label上字体大小颜色行间距的改变
利用NSMutableAttributedString实现label上字体大小颜色行间距的改变
1小时前 评论:0 阅读:738 黄海泼泼
ios label NSMutableAttributedString
   UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    label.text = @"随堂测验\n(学习完成后,开始测试)";//     使用斜杠n是字体显示为两行
    label.font =  [UIFont systemFontOfSize:10];
    label.numberOfLines = 0;//使用换行符号切莫忘记写这句
    label.textColor = [UIColor hexChangeFloat:@"A5A5A5"];

    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:label.text];//        利用属性字符串修改字体大小  颜色

    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[HuConfigration uiColorFromString:@"#D3D3D3"]
                          range:NSMakeRange(0, 4)];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont customFontSize:18]
                          range:NSMakeRange(0 , 4)];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];//        修改行间距
    [paragraphStyle setLineSpacing:4];
    [AttributedStr addAttribute:NSParagraphStyleAttributeName
                          value:paragraphStyle
                          range:NSMakeRange(0, [label.text length])];
   label.attributedText = AttributedStr;

    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];

效果图

 4F59517D-3B9A-4136-9163-FBAA42A501C9.png

收藏
0
sina weixin mail 回到顶部