给UIlabel设置不同字体 颜色 下划线

来源:互联网 发布:unity3d鼠标点击物体 编辑:程序博客网 时间:2024/05/20 05:23

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

    label.textColor = [UIColorgrayColor];

    NSMutableAttributedString *str = [[NSMutableAttributedStringalloc] initWithString:@"小明回复:终于可以回家了,太高兴了,哈哈哈哈哈哈。"];

    

    [str addAttribute:NSForegroundColorAttributeName

               value:[UIColorblueColor]

               range:NSMakeRange(0,5)];

    [str addAttribute:NSForegroundColorAttributeName

               value:[UIColorredColor]

               range:NSMakeRange(6,12)];

    [str addAttribute:NSForegroundColorAttributeName

               value:[UIColorblackColor]

               range:NSMakeRange(2,2)];

//下划线

[str addAttribute:(NSString *)kCTUnderlineStyleAttributeName

                           value:(id)[NSNumbernumberWithInt:kCTUnderlineStyleSingle]

                          range:NSMakeRange(0,4)];


    [str addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Arial-BoldItalicMT"size:30.0]range:NSMakeRange(0,5)];

    [str addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue-Bold"size:30.0]range:NSMakeRange(6,12)];

    [str addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Courier-BoldOblique"size:30.0]range:NSMakeRange(19,6)];

    label.attributedText = str;

0 0