Skip to content

Instantly share code, notes, and snippets.

@rosylxf
Forked from onmyway133/TextViewHighlighter.m
Created December 4, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosylxf/47f4dc4420d7a2a6edabdaeffde14478 to your computer and use it in GitHub Desktop.
Save rosylxf/47f4dc4420d7a2a6edabdaeffde14478 to your computer and use it in GitHub Desktop.
TextViewHighlighter
// 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