-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathNSMutableAttributedString+Typeset.m
85 lines (72 loc) · 2.61 KB
/
NSMutableAttributedString+Typeset.m
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// NSMutableAttributedString+Typeset.m
// Typeset
//
// Created by apple on 15/5/26.
// Copyright (c) 2015年 DeltaX. All rights reserved.
//
#import "NSMutableAttributedString+Typeset.h"
#import "TypesetKit.h"
#import <objc/runtime.h>
@implementation NSMutableAttributedString (Typeset)
- (TypesetKit *)typeset {
TypesetKit *typeset = objc_getAssociatedObject(self, @selector(typeset));
if (!typeset) {
typeset = [[TypesetKit alloc] init];
typeset.string = [[NSMutableAttributedString alloc] initWithAttributedString:self];
objc_setAssociatedObject(self, @selector(typeset), typeset, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return typeset;
}
- (NSMutableAttributedString *(^)(id))append {
return ^(id string) {
if (!string) return self;
NSAssert([string isKindOfClass:[NSString class]] ||
[string isKindOfClass:[NSAttributedString class]] ||
[string isKindOfClass:[NSMutableAttributedString class]], @"String passed into this method should be NSString,NSAttributedString or NSMutableAttributedString.");
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] init];
if ([string isKindOfClass:[NSString class]]) {
mas = [[NSMutableAttributedString alloc] initWithString:string];
} else if ([string isKindOfClass:[NSAttributedString class]]) {
mas = [[NSMutableAttributedString alloc] initWithAttributedString:string];
} else {
mas = (NSMutableAttributedString *)string;
}
[self appendAttributedString:mas];
return self;
};
}
- (NSMutableAttributedString *)bold {
return self.typeset.bold.string;
}
- (NSMutableAttributedString *(^)(UIColor *))color {
return ^(UIColor *color) {
return self.typeset.color(color).string;
};
}
- (NSMutableAttributedString *(^)(NSUInteger))hexColor {
return ^(NSUInteger hexColor) {
return self.typeset.hexColor(hexColor).string;
};
}
- (NSMutableAttributedString *(^)(NSUInteger))fontSize {
return ^(NSUInteger fontSize) {
return self.typeset.fontSize(fontSize).string;
};
}
- (NSMutableAttributedString *(^)(NSString *))fontName {
return ^(NSString *fontName) {
return self.typeset.fontName(fontName).string;
};
}
- (NSMutableAttributedString *(^)(NSString *, CGFloat))font {
return ^(NSString *fontName, CGFloat fontSize) {
return self.typeset.font(fontName, fontSize).string;
};
}
- (NSMutableAttributedString *(^)(UIFont *))exactFont {
return ^(UIFont *font) {
return self.typeset.exactFont(font).string;
};
}
@end