NSMutableAttributedString

NSMutableAttributedString

#import "ViewController.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, self.view.frame.size.height--)];
label.text = @"春种一粒粟,秋成万颗子。\n四海无闲田,农夫犹饿死。\n锄禾日当午,汗滴禾下土。\n谁知盘中餐,粒粒皆辛苦。";
[self.view addSubview:label];
NSRange rangeOne = [label.text rangeOfString:@"春种一粒粟"];
NSRange rangeTwo = [label.text rangeOfString:@"秋成万颗子"];
NSRange rangeThree = [label.text rangeOfString:@"四海无闲田"];
NSRange rangeFour = [label.text rangeOfString:@"农夫犹饿死"];
NSRange rangeFive = [label.text rangeOfString:@"锄禾日当午"];
NSRange rangeSix = [label.text rangeOfString:@"汗滴禾下土"];
NSRange rangeSeven = [label.text rangeOfString:@"谁知盘中餐"];
NSRange rangeEight = [label.text rangeOfString:@"粒粒皆辛苦"];
NSMutableAttributedString *attributedLabel = [[NSMutableAttributedString alloc] initWithString:label.text];
//字体
[attributedLabel addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:] range:NSMakeRange(, label.text.length)];
//段落
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = ; //行间距
paragraphStyle.alignment = NSTextAlignmentCenter;
[attributedLabel addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, label.text.length)];
//字体颜色
[attributedLabel addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeOne.location, rangeOne.length)];
//字体底色
[attributedLabel addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeTwo.location, rangeTwo.length)];
//删除线
[attributedLabel addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:] range:NSMakeRange(rangeThree.location, rangeThree.length)];
[attributedLabel addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeThree.location, rangeThree.length)];
//下划线
[attributedLabel addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:] range:NSMakeRange(rangeFour.location, rangeFour.length)];
[attributedLabel addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeFour.location, rangeFour.length)];
//文字间距
[attributedLabel addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:] range:NSMakeRange(rangeFive.location, rangeFive.length)];
//字体倾斜(正值右倾,负值左倾)
[attributedLabel addAttribute:NSObliquenessAttributeName value:[NSNumber numberWithFloat:0.5] range:NSMakeRange(rangeSix.location, rangeSix.length)];
//笔画宽度(正值中空,负值填充)
[attributedLabel addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:] range:NSMakeRange(rangeSeven.location, rangeSeven.length)];
//填充颜色
[attributedLabel addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeSeven.location, rangeSeven.length)];
//阴影效果
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor redColor];
shadow.shadowOffset = CGSizeMake(, );
shadow.shadowBlurRadius = ;
[attributedLabel addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(rangeEight.location, rangeEight.length)];
//自适应高
label.numberOfLines = ;
CGRect labelStringRect = [attributedLabel boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*, ) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect; label.attributedText = attributedLabel;
}

效果图如下:

NSMutableAttributedString(富文本)的简单使用-LMLPHP

04-18 12:01