iOS AttributeString 使用详解

http://blog.csdn.net/hello_hwc/article/details/46731991

一个简单的例子

绘制不同颜色不同字体的一个AttributeString,如图 
这里写图片描述 
代码

 UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * firstAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],};
    [firstPart setAttributes:firstAttributes range:NSMakeRange(0,firstPart.length)];
    NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@" Huang"];
    NSDictionary * secondAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor blueColor],};
    [secondPart setAttributes:secondAttributes range:NSMakeRange(0,secondPart.length)];
    [firstPart appendAttributedString:secondPart];
    Label.attributedText = firstPart;
1
2
3
4
5
6
7
8
9
10
先看看所有的Key

NSFontAttributeName; //字体,value是UIFont对象
NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
NSForegroundColorAttributeName;// 文字颜色,value是UIFont对象
NSBackgroundColorAttributeName;// 背景色,value是UIFont
NSLigatureAttributeName; //  字符连体,value是NSNumber
NSKernAttributeName; // 字符间隔
NSStrikethroughStyleAttributeName;//删除线,value是NSNumber
NSUnderlineStyleAttributeName;//下划线,value是NSNumber
NSStrokeColorAttributeName; //描绘边颜色,value是UIColor
NSStrokeWidthAttributeName; //描边宽度,value是NSNumber
NSShadowAttributeName; //阴影,value是NSShadow对象
NSTextEffectAttributeName; //文字效果,value是NSString
NSAttachmentAttributeName;//附属,value是NSTextAttachment 对象
NSLinkAttributeName;//链接,value是NSURL or NSString
NSBaselineOffsetAttributeName;//基础偏移量,value是NSNumber对象
NSUnderlineColorAttributeName;//下划线颜色,value是UIColor对象
NSStrikethroughColorAttributeName;//删除线颜色,value是UIColor
NSObliquenessAttributeName; //字体倾斜
NSExpansionAttributeName; //字体扁平化
NSVerticalGlyphFormAttributeName;//垂直或者水平,value是 NSNumber,0表示水平,1垂直
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
字体,颜色,背景色

涉及到的属性 
- NSFontAttributeName 
- NSForegroundColorAttributeName 
- NSBackgroundColorAttributeName

效果 
这里写图片描述

代码

UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:14]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
下划线

涉及到的两个属性

NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName
效果 
这里写图片描述

代码

    UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],                          NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),                             NSUnderlineColorAttributeName:[UIColor blueColor],};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
7
描边

涉及到的两个属性

NSStrokeColorAttributeName
NSStrokeWidthAttributeName
效果 
这里写图片描述

代码

    UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(2)};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
附属属性(例如图片)

涉及到的属性

NSAttachmentAttributeName
效果 
这里写图片描述 
代码

  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    //创建Attachment Str
    NSTextAttachment * attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"memoAccess"];
    attach.bounds = CGRectMake(0, 0, 20, 20);
    NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
    //添加
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    [mutableAttriStr appendAttributedString:imageStr];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
7
8
9
10
11
绘制风格

为什么要红字?因为这个属性真的很重要。

涉及到的属性

NSMutableParagraphStyle

效果(看起来很奇怪,仅仅为了展示某些功能) 
这里写图片描述

代码

@interface TestView : UIView

@end

@implementation TestView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    self.opaque = NO;
    return self;
}

// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"The anthor of this blog is wenchenhuang"];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentRight;
    paragraphStyle.headIndent = 4.0;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = 2.0;
    NSDictionary * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
    [attributeStr setAttributes:attributes range:NSMakeRange(0, attributeStr.length)];
    [attributeStr drawInRect:self.bounds];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    TestView  *test = [[TestView alloc] initWithFrame:CGRectMake(100, 100,100, 60)];
    [self.view addSubview:test];
1
2
阴影

涉及到的属性

NSShadowAttributeName
效果 
这里写图片描述

  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSShadow * shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor blueColor];
    shadow.shadowBlurRadius = 2.0;
    shadow.shadowOffset = CGSizeMake(1.0, 1.0);
    NSDictionary * attris = @{NSShadowAttributeName:shadow};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
7
8
9
10
文字效果

相关属性

NSTextEffectAttributeName 
这里写图片描述
代码

 UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
链接

相关属性

NSLinkAttributeName
例子

点击打开链接

@interface ViewController ()<UITextViewDelegate>

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    textView.scrollEnabled = NO;
    textView.editable = NO;
    textView.textContainer.lineFragmentPadding = 0;
    textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    textView.delegate = self;
    [self.view addSubview:textView];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    textView.attributedText = mutableAttriStr;
        // Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{
    return YES;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
文字连体

涉及到的属性

NSLigatureAttributeName
举例 
NSLigatureAttributeName 属性位@(0) 和@(1) 
这里写图片描述

这里写图片描述

字符间隔

相关属性

NSKernAttributeName
举例 字符间距拉大到4 
这里写图片描述 
对比下,默认的 
这里写图片描述

UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 120)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
    NSDictionary * attris = @{NSKernAttributeName:@(4),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
    Label.attributedText = mutableAttriStr;
1
2
3
4
5
6
7
baseline基础偏移量

相关属性 
- NSBaselineOffsetAttributeName

效果 
对比下偏移量为0 和20 


代码

  NSDictionary * attris = @{NSBaselineOffsetAttributeName:@(0),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};
1
2
字体倾斜

相关属性

NSObliquenessAttributeName 
效果 
这里写图片描述
代码

    NSDictionary * attris = @{NSObliquenessAttributeName:@(0.5),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};
1
2
字体扁平化

相关属性

NSExpansionAttributeName
效果 
这里写图片描述

代码

NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),
                              NSFontAttributeName:[UIFont systemFontOfSize:30]};
1
2


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值