iOS -多字体混合

时间:2021-10-23 07:03:47

label 加下划线

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 100)];
    label.backgroundColor = [UIColor redColor];
    label.numberOfLines = 3;
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"您的待办业务条fasdfasdfosadfjasdkljfklasjklfjasdlkjfklasdjklfjalskdjflkadsj"]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    
    label.attributedText = content;
    [self.view addSubview:label];

设置  大小不同的字体

       NSString * stra = [NSString stringWithFormat:@"0 %.2lf 0",[straaa doubleValue]];

NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:stra];

[str addAttribute:NSFontAttributeName value: kFontSize_24 range:NSMakeRange(0, 2)];

[str addAttribute:NSFontAttributeName value: kFontSize_100 range:NSMakeRange(2, str.length - 4)];

[str addAttribute:NSFontAttributeName value: kFontSize_24 range:NSMakeRange(str.length - 2, 2)];

[str addAttribute:NSForegroundColorAttributeName value: kColorBackground range:NSMakeRange(str.length - 2, 2)];

lable1.attributedText = str;