Last active
February 3, 2019 22:52
-
-
Save onmyway133/c1fb7f8bf26a0bd144a3 to your computer and use it in GitHub Desktop.
TextViewHighlighter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SO [UITextView - Highlight text with NSBackgroundColor - exclude line breaks](http://stackoverflow.com/questions/26558396/uitextview-text-highlighting-when-the-text-is-centered) | |
// Source | |
// TextViewHighlighter.h | |
#import <Foundation/Foundation.h> | |
@import UIKit; | |
@interface TextViewHighlighter : NSObject | |
- (instancetype)initWithTextView:(UITextView *)textView | |
text:(NSString *)text | |
textColor:(UIColor *)textColor | |
highlightedColor:(UIColor *)highlightedColor; | |
@property (nonatomic, assign) BOOL highlighted; | |
@end | |
// TextViewHighlighter.m | |
#import "TextViewHighlighter.h" | |
static NSString *const kLinebreakChar = @"\n"; | |
@interface TextViewHighlighter () | |
@property (nonatomic, strong) UITextView *textView; | |
@property (nonatomic, strong) UIColor *textColor; | |
@property (nonatomic, strong) UIColor *highlightedColor; | |
@property (nonatomic, copy) NSString *text; | |
@property (nonatomic, strong) NSAttributedString *highlightedAttributedString; | |
@property (nonatomic, strong) NSAttributedString *unhighlightedAttributedString; | |
@end | |
@implementation TextViewHighlighter | |
- (instancetype)initWithTextView:(UITextView *)textView | |
text:(NSString *)text | |
textColor:(UIColor *)textColor | |
highlightedColor:(UIColor *)highlightedColor { | |
self = [super init]; | |
if (self) { | |
_textView = textView; | |
_text = text; | |
_textColor = textColor; | |
_highlightedColor = highlightedColor; | |
[self setup]; | |
} | |
return self; | |
} | |
- (void)setup { | |
NSArray *rangeValues = [self rangeValuesOfLinebreaksInText:self.text]; | |
// Highlight | |
NSMutableAttributedString *highlighedAttributedString = [[NSMutableAttributedString alloc] initWithString:self.text]; | |
[highlighedAttributedString addAttributes:@{NSForegroundColorAttributeName: self.textColor, | |
NSBackgroundColorAttributeName: self.highlightedColor | |
} | |
range:NSMakeRange(0, self.text.length)]; | |
for (NSValue *rangeValue in rangeValues) { | |
NSRange range = [rangeValue rangeValue]; | |
[highlighedAttributedString addAttributes:@{NSBackgroundColorAttributeName: [UIColor clearColor], | |
} | |
range:range]; | |
} | |
self.highlightedAttributedString = highlighedAttributedString; | |
// Unhighlight | |
self.unhighlightedAttributedString = [[NSAttributedString alloc] | |
initWithString:self.text | |
attributes:@{NSForegroundColorAttributeName: self.textColor | |
}]; | |
} | |
- (void)setHighlighted:(BOOL)highlighted { | |
_highlighted = highlighted; | |
self.textView.attributedText = highlighted ? self.highlightedAttributedString : self.unhighlightedAttributedString; | |
} | |
#pragma mark - Helper | |
- (NSArray *)rangeValuesOfLinebreaksInText:(NSString *)text { | |
return [self rangeValuesOfCharacter:kLinebreakChar inText:text]; | |
} | |
- (NSArray *)rangeValuesOfCharacter:(NSString *)character inText:(NSString *)text { | |
NSMutableArray *array = [NSMutableArray array]; | |
NSRange searchRange = NSMakeRange(0, text.length); | |
NSRange foundRange; | |
while (searchRange.location < text.length) { | |
searchRange.length = text.length-searchRange.location; | |
foundRange = [text rangeOfString:character options:0 range:searchRange]; | |
if (foundRange.location != NSNotFound) { | |
[array addObject:[NSValue valueWithRange:foundRange]]; | |
searchRange.location = foundRange.location+foundRange.length; | |
} else { | |
break; | |
} | |
} | |
return array; | |
} | |
@end | |
// Usage | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSString *text = @"123\n456abc\n789"; | |
self.highlighter = [[TextViewHighlighter alloc] initWithTextView:self.textView | |
text:text | |
textColor:[UIColor blueColor] | |
highlightedColor:[UIColor blueColor]]; | |
self.highlighter.highlighted = NO; | |
} | |
- (IBAction)touched:(id)sender | |
{ | |
self.highlighter.highlighted = !self.highlighter.highlighted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment