NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSAttributeString 可以用来设置字体、段落样式,字体颜色,字体背景颜色,可以添加删除线、下划线,可以设置字间距、阴影、空心字、斜体、扁平化"];
[attributedString addAttribute:NSExpansionAttributeName value:@1 range:NSMakeRange(0, 17)]; // 扁平化
[attributedString addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(18, 8)];// 倾斜
//段落
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.firstLineHeadIndent = 80;// 首行缩进
paragraphStyle.headIndent = 25; // 其它行缩进
paragraphStyle.lineSpacing = 20; // 行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];// 段落
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 5; //模糊度
shadow.shadowColor = [UIColor redColor];
shadow.shadowOffset = CGSizeMake(1, 3);
[attributedString addAttribute:NSVerticalGlyphFormAttributeName value:@(0) range:NSMakeRange(27, 4)];
[attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(27, 4)];
[attributedString addAttribute:NSStrokeWidthAttributeName value:@(-3.0) range:NSMakeRange(32, 11)];// 边线宽度
[attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(32, 11)];//边线颜色,需要先设置边线宽度
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(44, 7)]; // 删除线
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(52, 3)]; // 下划线
[attributedString setAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldItalicMT" size:18], NSKernAttributeName:@(10),NSForegroundColorAttributeName:[UIColor redColor], NSBackgroundColorAttributeName:[UIColor yellowColor]} range:NSMakeRange(56, 20)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
label.numberOfLines = 0;
label.backgroundColor = [UIColor grayColor];
label.attributedText = attributedString;
[self.view addSubview:label];
#pragma mark
顶上
- (void)alignTop;
/**
* 改变行间距
*/
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;
/**
* 改变字间距
*/
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;
/**
* 改变行间距和字间距
*/
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;
- (void)alignTop {
CGSize fontSize = [self.text sizeWithFont:[UIFont systemFontOfSize:19.0 weight:0.2]];
double finalHeight = fontSize.height * self.numberOfLines;
double finalWidth = self.frame.size.width; //expected width of label
CGSize theStringSize = [self.text sizeWithFont:[UIFont systemFontOfSize:19.0 weight:0.2] constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text = [self.text stringByAppendingString:@"\n "];
}
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
label.attributedText = attributedString;
[label sizeToFit];
}