NSString+BBAAttributed.h
2.19 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// NSString+BBAAttributed.h
//
//
// Created by linling on 15/6/27.
// Copyright (c) 2015年 baidu. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BBAAttributedStringMaker.h"
/** 快捷创建属性字符串,使用方式如下:
NSString *info = @"hello world";
1,创建字体大小为14、颜色为红色的属性字符串:
NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
}];
2,创建整体字体大小为14、颜色为红色,只有hello部分的字体大小为20、颜色为白色的属性字符串:
NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
maker.subString(@"hello", ^(BBAAttributedStringMaker *maker){
maker.font([UIFont systemFontOfSize:20]).color([UIColor whiteColor]);
});
}];
3,创建整体字体大小为14、颜色为红色,只有最后2个字符的字体大小为20、颜色为白色的属性字符串:
NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
maker.fromIndex(info.length - 2, ^(BBAAttributedStringMaker *maker){
maker.font([UIFont systemFontOfSize:20]).color([UIColor whiteColor]);
});
}];
4,创建整体字体大小为14、颜色为红色,hello部分的字符间距为10,world部分的字符间距为20的属性字符串:
NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
maker.subString(@"hello", ^(BBAAttributedStringMaker *maker){
maker.wordSpace(10);
});
maker.subString(@"world", ^(BBAAttributedStringMaker *maker){
maker.wordSpace(20);
});
}];
*/
@interface NSString (BBAAttributed)
- (NSAttributedString *)bba_attrRender:(void(^)(BBAAttributedStringMaker *maker))block;
/// 返回指定字体、大小下当前文本所需的行数
- (NSUInteger)bba_numberOfLineForFont:(UIFont *)font size:(CGSize)size;
@end