字符串加下划线和改变字符串某个字段的颜色和大小

 //给UILabel文字加下划线
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 100)];
    label.numberOfLines = 3;
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"您的待办业务"]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    label.attributedText = content;
    [self.view addSubview:label];
    
    //string中的keyWord改变颜色
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 300, 100)];
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:12], NSFontAttributeName, nil];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSRange keyWordRang = [label.text rangeOfString:@"您"];
    [attributedString addAttributes:attrs range:keyWordRang];
    label2.attributedText = attributedString;
    [self.view addSubview:label2];

你可能感兴趣的:(字符串加下划线和改变字符串某个字段的颜色和大小)