label赋值属性字符串

来源:互联网 发布:js获取button按钮的值 编辑:程序博客网 时间:2024/05/04 18:38

直接上代码

UILabel *lab = [[UILabelalloc]init];

    lab.backgroundColor = [UIColorwhiteColor];

    lab.frame =CGRectMake(10.0,40.0,400, 100);

    [self.viewaddSubview:lab];

    // label添加属性字符串

    NSMutableAttributedString *attStr = [[NSMutableAttributedStringalloc]initWithString:@"可变属性字符串例子,谢谢"];

    // 颜色

    [attStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(1,2)];

    // 字体

    [attStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:10.0]range:NSMakeRange(4,3)];

    // 下划线

    [attStr addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInt:NSUnderlineStyleSingle]range:NSMakeRange(5,4)];

    lab.attributedText = attStr;


补充:

// 行间距设置

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];

    [paragraphStylesetLineSpacing:20];

    [attStraddAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle range:NSMakeRange(0, [attStrlength])];




0 0
原创粉丝点击