-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathBoldStyle.mm
More file actions
125 lines (110 loc) · 4.21 KB
/
Copy pathBoldStyle.mm
File metadata and controls
125 lines (110 loc) · 4.21 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#import "StyleHeaders.h"
#import "EnrichedTextInputView.h"
#import "FontExtension.h"
#import "OccurenceUtils.h"
@implementation BoldStyle {
EnrichedTextInputView *_input;
}
+ (StyleType)getStyleType { return Bold; }
- (instancetype)initWithInput:(id)input {
self = [super init];
_input = (EnrichedTextInputView *)input;
return self;
}
- (void)applyStyle:(NSRange)range {
BOOL isStylePresent = [self detectStyle:range];
if(range.length >= 1) {
isStylePresent ? [self removeAttributes:range] : [self addAttributes:range];
} else {
isStylePresent ? [self removeTypingAttributes] : [self addTypingAttributes];
}
}
- (void)addAttributes:(NSRange)range {
[_input->textView.textStorage beginEditing];
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:range options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIFont *font = (UIFont *)value;
if(font != nullptr) {
UIFont *newFont = [font setBold];
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
}
}
];
[_input->textView.textStorage endEditing];
}
- (void)addTypingAttributes {
UIFont *currentFontAttr = (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
if(currentFontAttr != nullptr) {
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
newTypingAttrs[NSFontAttributeName] = [currentFontAttr setBold];
_input->textView.typingAttributes = newTypingAttrs;
}
}
- (void)removeAttributes:(NSRange)range {
[_input->textView.textStorage beginEditing];
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:range options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIFont *font = (UIFont *)value;
if(font != nullptr) {
UIFont *newFont = [font removeBold];
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
}
}
];
[_input->textView.textStorage endEditing];
}
- (void)removeTypingAttributes {
UIFont *currentFontAttr = (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
if(currentFontAttr != nullptr) {
NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
newTypingAttrs[NSFontAttributeName] = [currentFontAttr removeBold];
_input->textView.typingAttributes = newTypingAttrs;
}
}
- (BOOL)boldHeadingConflictsInRange:(NSRange)range type:(StyleType)type {
if(type == H1) {
if(![_input->config h1Bold]) { return NO; }
} else if(type == H2) {
if(![_input->config h2Bold]) { return NO; }
} else if(type == H3) {
if(![_input->config h3Bold]) { return NO; }
}
id<BaseStyleProtocol> headingStyle = _input->stylesDict[@(type)];
return range.length > 0
? [headingStyle anyOccurence:range]
: [headingStyle detectStyle:range];
}
- (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
UIFont *font = (UIFont *)value;
return font != nullptr && [font isBold] && ![self boldHeadingConflictsInRange:range type:H1] && ![self boldHeadingConflictsInRange:range type:H2] && ![self boldHeadingConflictsInRange:range type:H3];
}
- (BOOL)detectStyle:(NSRange)range {
if(range.length >= 1) {
return [OccurenceUtils detect:NSFontAttributeName withInput:_input inRange:range
withCondition: ^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
} else {
return [OccurenceUtils detect:NSFontAttributeName withInput:_input atIndex:range.location checkPrevious:NO
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
}
- (BOOL)anyOccurence:(NSRange)range {
return [OccurenceUtils any:NSFontAttributeName withInput:_input inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
- (NSArray<StylePair *> *_Nullable)findAllOccurences:(NSRange)range {
return [OccurenceUtils all:NSFontAttributeName withInput:_input inRange:range
withCondition:^BOOL(id _Nullable value, NSRange range) {
return [self styleCondition:value :range];
}
];
}
@end