ios富文本属性

时间:2023-02-01 00:20:55

@interfaceViewController ()@property (weak,nonatomic

IBOutletUILabel *stringLabel;@end@implementationViewController

- (void)viewDidLoad {  

[super viewDidLoad];  

NSString *string = _stringLabel.text;//初始化属性字符串  

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];//字体类型属性  

NSDictionary *BoldFontAS = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]}; [attributedString addAttributes:BoldFontAS range:[string rangeOfString:@"BoldFont"]];//字体颜色属性  

NSDictionary *RedFondAS = @{NSForegroundColorAttributeName :[UIColor redColor]};  

[attributedString addAttributes:RedFondAS range:[string rangeOfString:@"RedFont"]];//字体背景颜色和字体颜色属性NSDictionary *BuleBackgroundAS = @{NSBackgroundColorAttributeName:[UIColor blueColor], NSForegroundColorAttributeName:[UIColor whiteColor]};  

[attributedString addAttributes:BuleBackgroundAS range:[string rangeOfString:@"BuleBackground"]];//字体下划线与字体下划线颜色属性  

NSDictionary *UnderlineAS = @{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName:[UIColor greenColor]};  

[attributedString addAttributes:UnderlineAS range:[string rangeOfString:@"Underline"]];//字体阴影属性  

NSShadow *shadow = [[NSShadow alloc] init];  

shadow.shadowOffset = CGSizeMake(2,2);  

shadow.shadowColor = [UIColor orangeColor];  

NSDictionary *ShadowAS = @{NSShadowAttributeName:shadow};  

[attributedString addAttributes:ShadowAS range:[string rangeOfString:@"Shadow"]];//设置Label的字符串属性 _stringLabel.attributedText = attributedString;}