富文本2

实现效果:


富文本2_第1张图片
富文本.png

实现代码

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *label1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建富文本对象
    NSMutableAttributedString *attrrbute1 = [[NSMutableAttributedString alloc] initWithString:self.label1.text];
    
    //设置字体大小
    [attrrbute1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:29] range:NSMakeRange(5, 4)];

    //设置字体颜色
    [attrrbute1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 4)];
    
    //设置字体背景颜色
    [attrrbute1 addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(15, 5)];
    
    //设置删除线
    [attrrbute1 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(31, 10)];
    
    //设置下划线
    [attrrbute1 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:2] range:NSMakeRange(40, 10)];
    
    //一下两者必须结合使用
    //设置空心字颜色
    [attrrbute1 addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(50, 10)];
    //空心字边框颜色
    [attrrbute1 addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(50, 10)];

    //将富文本属性添加到控件上
    self.label1.attributedText = attrrbute1;

    
}

@end```

你可能感兴趣的:(富文本2)