Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d684aa

Browse files
author
Jacky Wu
committed
use attributed string by default and add helper methods
1 parent ace9bd2 commit 8d684aa

3 files changed

Lines changed: 136 additions & 13 deletions

File tree

Source/SLKTextView+SLKAdditions.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ NS_ASSUME_NONNULL_BEGIN
4646
*/
4747
- (void)slk_insertTextAtCaretRange:(NSString *)text;
4848

49+
/**
50+
Insert a string at the caret's position with stylization from the attributes.
51+
52+
@param text The string to be appended to the current text.
53+
@param attributes The attributes used to stylize the text.
54+
*/
55+
- (void)slk_insertTextAtCaretRange:(NSString *)text
56+
withAttributes:(NSDictionary<NSString *, id> *)attributes;
57+
4958
/**
5059
Adds a string to a specific range.
5160
@@ -56,6 +65,55 @@ NS_ASSUME_NONNULL_BEGIN
5665
*/
5766
- (NSRange)slk_insertText:(NSString *)text inRange:(NSRange)range;
5867

68+
/**
69+
Adds a string to a specific range, with stylization from the attributes.
70+
71+
@param text The string to be appended to the current text.
72+
@param attributes The attributes used to stylize the text.
73+
@param range The range where to insert text.
74+
75+
@return The range of the newly inserted text.
76+
*/
77+
- (NSRange)slk_insertText:(NSString *)text
78+
withAttributes:(NSDictionary<NSString *, id> *)attributes
79+
inRange:(NSRange)range;
80+
81+
/**
82+
Sets the text attributes for the attributed string in the provided range.
83+
84+
@param attributes The attributes used to style NSAttributedString class.
85+
@param range The range of the text that needs to be stylized by the given attributes.
86+
87+
@return The attributedText with updated attributes.
88+
*/
89+
- (NSAttributedString *)slk_setAttribute:(NSDictionary<NSString *, id> *)attributes
90+
inRange:(NSRange)range;
91+
92+
/**
93+
Inserts an attributed string at the caret's position.
94+
95+
@param text The string to be appended to the current text.
96+
*/
97+
- (void)slk_insertAttributedTextAtCaretRange:(NSAttributedString *)attributedText;
98+
99+
/**
100+
Adds an attributed string to a specific range.
101+
102+
@param text The string to be appended to the current text.
103+
@param range The range where to insert text.
104+
105+
@return The range of the newly inserted text.
106+
*/
107+
- (NSRange)slk_insertAttributedText:(NSAttributedString *)attributedText inRange:(NSRange)range;
108+
109+
/**
110+
Remove all attributed string attributes from the text for the given range
111+
112+
@param range The range to remove the attributes.
113+
*/
114+
115+
- (void)slk_clearAllAttributesInRange:(NSRange)range;
116+
59117
/**
60118
Registers the current text for future undo actions.
61119

Source/SLKTextView+SLKAdditions.m

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

Source/SLKTextView.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,25 @@ - (void)setSelectedTextRange:(UITextRange *)selectedTextRange
450450

451451
- (void)setText:(NSString *)text
452452
{
453+
if (!text) {
454+
return;
455+
}
456+
453457
// Registers for undo management
454458
[self slk_prepareForUndo:@"Text Set"];
455459

456-
[super setText:text];
460+
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text];
461+
462+
[self setAttributedText:attributedText];
457463

458464
[[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:self];
459465
}
460466

467+
- (NSString *)text
468+
{
469+
return self.attributedText.string;
470+
}
471+
461472
- (void)setAttributedText:(NSAttributedString *)attributedText
462473
{
463474
// Registers for undo management

0 commit comments

Comments
 (0)