@@ -13,7 +13,8 @@ @implementation SLKTextView (SLKAdditions)
1313- (void )slk_clearText : (BOOL )clearUndo
1414{
1515 // Important to call self implementation, as SLKTextView overrides setText: to add additional features.
16- [self setText: nil ];
16+
17+ [self setAttributedText: nil ];
1718
1819 if (self.undoManagerEnabled && clearUndo) {
1920 [self .undoManager removeAllActions ];
@@ -66,34 +67,79 @@ - (void)slk_insertTextAtCaretRange:(NSString *)text
6667 self.selectedRange = NSMakeRange (range.location , 0 );
6768}
6869
70+ - (void )slk_insertTextAtCaretRange : (NSString *)text
71+ withAttributes : (NSDictionary <NSString *, id> *)attributes
72+ {
73+ NSRange range = [self slk_insertText: text withAttributes: attributes inRange: self .selectedRange];
74+ self.selectedRange = NSMakeRange (range.location , 0 );
75+ }
76+
6977- (NSRange )slk_insertText : (NSString *)text inRange : (NSRange )range
7078{
71- // Skip if the text is empty
72- if (text.length == 0 ) {
79+ NSAttributedString *attributedText = [[NSAttributedString alloc ] initWithString: text];
80+ return [self slk_insertAttributedText: attributedText inRange: range];
81+ }
82+
83+ - (NSRange )slk_insertText : (NSString *)text
84+ withAttributes : (NSDictionary <NSString *, id> *)attributes
85+ inRange : (NSRange )range
86+ {
87+ NSAttributedString *attributedText = [[NSAttributedString alloc ] initWithString: text attributes: attributes];
88+ return [self slk_insertAttributedText: attributedText inRange: range];
89+ }
90+
91+ - (NSAttributedString *)slk_setAttribute : (NSDictionary <NSString *, id> *)attributes
92+ inRange : (NSRange )range
93+ {
94+ NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc ] initWithAttributedString: self .attributedText];
95+
96+ [attributedText setAttributes: attributes range: range];
97+ [self setAttributedText: attributedText];
98+ return self.attributedText ;
99+ }
100+
101+ - (void )slk_insertAttributedTextAtCaretRange : (NSAttributedString *)attributedText
102+ {
103+ NSRange range = [self slk_insertAttributedText: attributedText inRange: self .selectedRange];
104+ self.selectedRange = NSMakeRange (range.location , 0 );
105+ }
106+
107+ - (NSRange )slk_insertAttributedText : (NSAttributedString *)attributedText inRange : (NSRange )range
108+ {
109+ // Skip if the attributed text is empty
110+ if (attributedText.length == 0 ) {
73111 return NSMakeRange (0 , 0 );
74112 }
75113
76114 // Registers for undo management
77- [self slk_prepareForUndo: @" Text appending" ];
115+ [self slk_prepareForUndo: @" Attributed text appending" ];
78116
79117 // Append the new string at the caret position
80118 if (range.length == 0 )
81119 {
82- NSString *leftString = [self .text substringToIndex: range.location];
83- NSString *rightString = [self .text substringFromIndex: range.location];
120+ NSAttributedString *leftAttributedString = [self .attributedText attributedSubstringFromRange: NSMakeRange (0 , range.location)];
84121
85- self.text = [NSString stringWithFormat: @" %@%@%@ " , leftString, text, rightString];
122+ NSAttributedString *rightAttributedString = [self .attributedText attributedSubstringFromRange: NSMakeRange (range.location, self .attributedText.length-range.location)];
123+
124+ NSMutableAttributedString *newAttributedText = [NSMutableAttributedString new ];
125+ [newAttributedText appendAttributedString: leftAttributedString];
126+ [newAttributedText appendAttributedString: attributedText];
127+ [newAttributedText appendAttributedString: rightAttributedString];
128+
129+ [self setAttributedText: newAttributedText];
130+ range.location += attributedText.length ;
86131
87- range.location += text.length ;
88-
89132 return range;
90133 }
91134 // Some text is selected, so we replace it with the new text
92135 else if (range.location != NSNotFound && range.length > 0 )
93136 {
94- self.text = [self .text stringByReplacingCharactersInRange: range withString: text];
95-
96- range.location += text.length ;
137+ NSMutableAttributedString *mutableAttributeText = [[NSMutableAttributedString alloc ] initWithAttributedString: self .attributedText];
138+
139+ [mutableAttributeText replaceCharactersInRange: range withAttributedString: attributedText];
140+
141+ [self setAttributedText: mutableAttributeText];
142+ range.location += self.attributedText .length ;
97143
98144 return range;
99145 }
@@ -102,6 +148,14 @@ - (NSRange)slk_insertText:(NSString *)text inRange:(NSRange)range
102148 return self.selectedRange ;
103149}
104150
151+ - (void )slk_clearAllAttributesInRange : (NSRange )range
152+ {
153+ NSMutableAttributedString *mutableAttributedText = [[NSMutableAttributedString alloc ] initWithAttributedString: self .attributedText];
154+
155+ [mutableAttributedText setAttributes: nil range: range];
156+ [self setAttributedText: mutableAttributedText];
157+ }
158+
105159- (void )slk_prepareForUndo : (NSString *)description
106160{
107161 if (!self.undoManagerEnabled ) {
0 commit comments