说明:
NSAttributedString 可以非常方便的实现文字排版和图文混排功能. 共有21中效果(API), 本文将较详细的介绍21种的属性的使用
注: 本博客由@凡俊编写, @Scott, @春雨 审核, 若转载此文章,请注明出处和作者
核心API:
类: NSAttributedString, NSMutableAttributedString
API:
/**
* API: Character Attributes , NSAttributedString 共有21个属性
*
* 1. NSFontAttributeName ->设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
*
*
* 2. NSParagraphStyleAttributeName ->设置文本段落排版格式,取值为 NSParagraphStyle 对象(详情见下面的API说明)
*
*
* 3. NSForegroundColorAttributeName ->设置字体颜色,取值为 UIColor对象,默认值为黑色
*
*
* 4. NSBackgroundColorAttributeName ->设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
*
*
* 5. NSLigatureAttributeName ->设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
*
*
* 6. NSKernAttributeName ->设置字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
*
*
* 7. NSStrikethroughStyleAttributeName ->设置删除线,取值为 NSNumber 对象(整数)
*
*
* 8. NSStrikethroughColorAttributeName ->设置删除线颜色,取值为 UIColor 对象,默认值为黑色
*
*
* 9. NSUnderlineStyleAttributeName ->设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
*
*
* 10. NSUnderlineColorAttributeName ->设置下划线颜色,取值为 UIColor 对象,默认值为黑色
*
*
* 11. NSStrokeWidthAttributeName ->设置笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
*
*
* 12. NSStrokeColorAttributeName ->填充部分颜色,不是字体颜色,取值为 UIColor 对象
*
*
* 13. NSShadowAttributeName ->设置阴影属性,取值为 NSShadow 对象
*
*
* 14. NSTextEffectAttributeName ->设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
*
*
* 15. NSBaselineOffsetAttributeName ->设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
*
*
* 16. NSObliquenessAttributeName ->设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
*
*
* 17. NSExpansionAttributeName ->设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
*
*
* 18. NSWritingDirectionAttributeName ->设置文字书写方向,从左向右书写或者从右向左书写
*
*
* 19. NSVerticalGlyphFormAttributeName ->设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
*
*
* 20. NSLinkAttributeName ->设置链接属性,点击后调用浏览器打开指定URL地址
*
*
* 21. NSAttachmentAttributeName ->设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
*
*/
/**
* API: NSParagraphStyleAttributeName
*
* 值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。
*
* NSMutableParagraphStyle与NSParagraphStyle包括一下属性
* alignment ->对齐方式
* firstLineHeadIndent ->首行缩进
* headIndent ->缩进
* tailIndent ->尾部缩进
* lineBreakMode ->断行方式
* maximumLineHeight ->最大行高
* minimumLineHeight ->最低行高
* lineSpacing ->行距
* paragraphSpacing ->段距
* paragraphSpacingBefore ->段首空间
* baseWritingDirection ->句子方向
* lineHeightMultiple ->可变行高,乘因数。
* hyphenationFactor ->连字符属性
*/
代码
- (void)creatTitleLabel {
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 320, 400)];
self.titleLabel.numberOfLines = 0;
self.titleLabel.layer.borderColor = [UIColor grayColor].CGColor;
self.titleLabel.layer.borderWidth = 0.5;
self.titleLabel.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:self.titleLabel];
NSString *string = @"An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. ";
#pragma mark - NSMutableAttributedString 创建
NSAttributedString *attStri = [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]}];
NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:string];
#pragma mark ** 1. NSFontAttributeName 设置字体属性
NSRange font_range = [string rangeOfString:@"An"];
[mAttStri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:font_range];
[mAttStri addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:17.0] range:NSMakeRange(10, 10)];
#pragma mark ** 2. NSParagraphStyleAttributeName 设置文本段落排版格式
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.firstLineHeadIndent = 20;
style.lineSpacing = 10;
[mAttStri addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, mAttStri.length / 2)];
#pragma mark ** 3. NSForegroundColorAttributeName 设置字体颜色
[mAttStri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, mAttStri.length)];
#pragma mark ** 4. NSBackgroundColorAttributeName 设置字体所在区域背景颜色
[mAttStri addAttribute:NSBackgroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 20)];
#pragma mark ** 5. NSLigatureAttributeName 设置连体属性
[mAttStri addAttribute:NSLigatureAttributeName value:@0 range:NSMakeRange(0, mAttStri.length)];
#pragma mark ** 6. NSKernAttributeName 设置字符间距
[mAttStri addAttribute:NSKernAttributeName value:@3 range:NSMakeRange(0, mAttStri.length)];
#pragma mark ** 7. NSStrikethroughStyleAttributeName 设置删除线
[mAttStri addAttribute:NSStrikethroughStyleAttributeName value:@3 range:NSMakeRange(3, 7)];
#pragma mark ** 8. NSStrikethroughColorAttributeName 设置删除线颜色
[mAttStri addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:NSMakeRange(3, 3)];
#pragma mark ** 9. NSUnderlineStyleAttributeName 设置下划线
[mAttStri addAttribute:NSUnderlineStyleAttributeName value:@2 range:NSMakeRange(6, 5)];
#pragma mark ** 10. NSUnderlineColorAttributeName 设置下划线颜色
[mAttStri addAttribute:NSUnderlineColorAttributeName value:[UIColor blackColor] range:NSMakeRange(6, 5)];
#pragma mark ** 11. NSStrokeWidthAttributeName 设置笔画宽度(粗细)
[mAttStri addAttribute:NSStrokeWidthAttributeName value:@10 range:NSMakeRange(50, 30)];
#pragma mark ** 12. NSStrokeColorAttributeName 填充部分颜色,
[mAttStri addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(50, 20)];
#pragma mark ** 13. NSShadowAttributeName 设置阴影属性
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(10, 10);
shadow.shadowColor = [UIColor greenColor];
[mAttStri addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(20, 10)];
#pragma mark ** 14. NSTextEffectAttributeName 设置文本特殊效果
[mAttStri addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(80, 10)];
#pragma mark ** 15. NSBaselineOffsetAttributeName 设置基线偏移值
[mAttStri addAttribute:NSBaselineOffsetAttributeName value:@5 range:NSMakeRange(112, 10)];
#pragma mark ** 16. NSObliquenessAttributeName 设置字形倾斜度取值为 NSNumber (float),正值右倾,负值左倾
[mAttStri addAttribute:NSObliquenessAttributeName value:@0.8 range:NSMakeRange(135, 15)];
#pragma mark ** 17. NSExpansionAttributeName 设置文本横向拉伸属性
NSRange range = [string rangeOfString:@"An association of"];
[mAttStri addAttribute:NSExpansionAttributeName value:@1.0 range:range];
#pragma mark ** 18. NSWritingDirectionAttributeName 设置文字书写方向
NSRange rang2 = [string rangeOfString:@"characters and their"];
[mAttStri addAttribute:NSWritingDirectionAttributeName value:@[@3] range:rang2];
#pragma mark ** 19. NSVerticalGlyphFormAttributeName 设置文字排版方向
[mAttStri addAttribute:NSVerticalGlyphFormAttributeName value:@1 range:NSMakeRange(1, 10)];
#pragma mark ** 20. NSLinkAttributeName 设置链接属性
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 450, 320, 60)];
[self.view addSubview:textView];
textView.backgroundColor = [UIColor lightGrayColor];
NSString *strLink = @"百度链接";
NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:strLink attributes:@{NSLinkAttributeName: [NSURL URLWithString:@"http://www.baidu.com"]}];
textView.editable = NO;
textView.delegate = self;
textView.attributedText = attStr;
#pragma mark ** 21. NSAttachmentAttributeName 设置文本附件
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 550, 320, 60)];
label.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label];
NSString *stiAtt = @"百度";
NSTextAttachment *attach = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attach.bounds = CGRectMake(0, 0, 50, 50);
attach.image = [UIImage imageNamed:@"baidu.jpg"];
NSAttributedString *strAtt = [NSAttributedString attributedStringWithAttachment:attach];
NSMutableAttributedString *strMatt = [[NSMutableAttributedString alloc] initWithString:stiAtt];
[strMatt insertAttributedString:strAtt atIndex:1];
label.attributedText = strMatt;
self.titleLabel.attributedText = mAttStri;
[self.titleLabel sizeToFit];
}
#pragma mark - textView delegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
NSLog(@"%s", __func__);
NSLog(@"url: %@", URL);
return YES;
}
Demo的链接地址
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)