NSAttributedString是一种带有属性的字符串,通过该类我们可以灵活操作和呈现多种样式的文字数据,常用语文字排版和图文混排。
NSAttributedString和NSMutableAttributedString类似NSArray和NSMutableArray,NSAttributedString创建成功后的属性是不可变的,而NSMutableAttributedString创建成功后的属性是可变的。
NSAttributedString共有21个属性:
1.NSFontAttributeName // 设置字体属性,默认是 Helvetica(Neue) 12
2.NSParagraphStyleAttributeName // 设置文本段落排版格式 默认是NSParagraphStyle对象([NSParagraphStyle defaultParagraphStyle]);
3.NSForegroundColorAttributeName // 设置文本颜色,默认是黑色
4.NSBackgroundColorAttributeName // 设置字体背景色,默认是没有颜色
5.NSLigatureAttributeName //设置连体属性,取值为NSNumber ,默认是1,表示使用默认的连体字符,0是没有连体字符
6.NSKernAttributeName //设置字符间距,取值为NSNumber,正值间距加宽,负值间距变窄
7.NSStrikethroughStyleAttributeName //设置删除线,取值为NSNumber
8.NSUnderlineStyleAttributeName // 设置下划线,取值为NSNumber
9.NSStrokeColorAttributeName //设置笔画颜色,取值是UIColor
10.NSStrokeWidthAttributeName //设置笔画宽度(粗细),取值为 NSNumber,负值填充效果,正值中空效果
11.NSShadowAttributeName //设置阴影属性,取值为 NSShadow
12.NSTextEffectAttributeName //设置文本特殊效果,取值为 NSString
13.NSAttachmentAttributeName //设置文本附件,取值为NSTextAttachment,常用于文字图片混排
14.NSLinkAttributeName //设置链接属性,点击后调用浏览器打开指定URL地址
15.NSBaselineOffsetAttributeName //设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
16.NSUnderlineColorAttributeName //设置下划线颜色,取值为 UIColor 对象,默认值为黑色
17.NSStrikethroughColorAttributeName //设置删除线颜色,取值为 UIColor 对象,默认值为黑色
18.NSObliquenessAttributeName //设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
19.NSExpansionAttributeName //设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文
20.NSWritingDirectionAttributeName //设置文字书写方向,从左向右书写或者从右向左书写 ,取值:LRE, RLE, LRO, and RLO,可见API
21.NSVerticalGlyphFormAttributeName //设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
//
// ViewController.m
// NSAttributedStringDemo
//
// Created by york on 16/4/11.
// Copyright © 2016年 裴杨威. All rights reserved.
//
#define DEVICE_HEIGHT [UIScreen mainScreen].bounds.size.height
#define DEVICE_WIDTH [UIScreen mainScreen].bounds.size.width
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UILabel *showLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *str = @"CSDNhttp://www.csdn.net(隶属北京创新乐知信息技术有限公司)创立于1999年,是中国最大的IT社区和服务平台,为中国的软件开发者和IT从业者提供知识传播、职业发展、软件开发等全生命周期服务,满足他们在职业发展中学习及共享知识和信息、建立职业发展社交圈、通过软件开发实现技术商业化等刚性需求。拥有超过3000万注册会员(其中活跃会员800万)、50万注册企业及合作伙伴。";
NSAttributedString *astr = [[NSAttributedString alloc] initWithString:str];
NSMutableAttributedString *attributStr = [[NSMutableAttributedString alloc] initWithAttributedString:astr];
//属性1,设置字体属性,示例:
[attributStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"ArialRoundedMTBold" size:24.0f] range:NSMakeRange(0, 4)];
//属性2,设置文本段落排版格式,示例:
//属性3,设置文本颜色,示例:
[attributStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(7, 14)];
//属性4,设置字体背景色,示例:
[attributStr addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(25, 4)];
//属性5,设置连体属性,示例:*
[attributStr addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(attributStr.length-2, 2)];
//属性6,设置字符间距,示例:
[attributStr addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:10] range:NSMakeRange(0, 4)];
//属性7,设置删除线,示例:
[attributStr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(147, 4)];
//属性8,设置下划线,示例:
[attributStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(157, 4)];
//属性9,设置笔画颜色,示例:*
[attributStr addAttribute:NSStrokeColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(80, 10)];
//属性10,设置笔画宽度(粗细),示例:
[attributStr addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:-5] range:NSMakeRange(100, 4)];
//属性11,设置阴影属性,示例:
// NSShadow 共有3个属性:阴影颜色,模糊半径和偏移
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(3, 3);
shadow.shadowBlurRadius = 0.5;
shadow.shadowColor = [UIColor grayColor]; //阴影颜色
[attributStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(110, 4)];
//属性12,设置文本特殊效果,示例:
[attributStr addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(120, 4)];
//属性14,设置链接属性,示例:
[attributStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:[str substringWithRange:NSMakeRange(4, 19)]] range:NSMakeRange(4, 19)];
//属性15,设置基线偏移值,示例:
[attributStr addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithInt:10] range:NSMakeRange(str.length-10, 10)];
//属性16,设置下划线颜色,示例:
[attributStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(147, 4)];
//属性17,设置删除线颜色,示例:
[attributStr addAttribute:NSUnderlineColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(157, 4)];
//属性18,设置字形倾斜度,示例:
[attributStr addAttribute:NSObliquenessAttributeName value:@0.5 range:NSMakeRange(133, 10)];
//属性19,设置文本横向拉伸属性,示例:
[attributStr addAttribute:NSExpansionAttributeName value:@0.5 range:NSMakeRange(123, 10)];
//属性20,设置文字书写方向,示例:
[attributStr addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionLeftToRight)] range:NSMakeRange(str.length-10, 10)];
//属性21,设置文字排版方向,示例:
[attributStr addAttribute:NSVerticalGlyphFormAttributeName value:@0 range:NSMakeRange(0, str.length)];
self.showLabel.attributedText = attributStr;
}
- (UILabel *)showLabel
{
if (!_showLabel) {
_showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, DEVICE_WIDTH, DEVICE_HEIGHT-200)];
_showLabel.layer.borderWidth = 1;
_showLabel.numberOfLines = 0;
_showLabel.layer.borderColor = [UIColor grayColor].CGColor;
_showLabel.userInteractionEnabled = YES;
[self.view addSubview:_showLabel];
}
return _showLabel;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如果需要的话,也可以通过 NSDictionary来先设置需要的属性,然后使用NSAttributedString的- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs方法来设置。