高亮顯示單詞
需求:獲取點(diǎn)擊 UITextView 的某個(gè)單詞,高亮顯示栅受。
思路:創(chuàng)建 textView 的子類每辟。先獲取 touchPoint塔淤,然后用 textView.layoutManager 的 characterIndexForPoint: 方法獲取點(diǎn)擊的字符的位置,從位置向前后遍歷直到遇到空格就能獲取點(diǎn)擊的單詞的 NSRange斥杜,有了 range 就能獲取點(diǎn)擊的單詞虱颗,或者高亮顯示。
子類關(guān)鍵代碼:
// WordTextView.m蔗喂,繼承自 UITextView忘渔。
// 重寫(xiě)觸摸開(kāi)始函數(shù)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 獲取當(dāng)前觸摸位置的字符所屬的字母(提示:觸摸位置需向下調(diào)整10個(gè)點(diǎn),以便與文本元素對(duì)齊)
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
touchPoint.y -= 10;
// 獲取點(diǎn)擊的字母的位置
NSInteger characterIndex = [self.layoutManager characterIndexForPoint:touchPoint inTextContainer:self.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];
// 獲取單詞的范圍缰儿。range 由起始位置和長(zhǎng)度構(gòu)成畦粮。
NSRange range = [self getWordRange:characterIndex];
// 高亮單詞
[self modifyAttributeInRange:range];
//調(diào)用父類的方法
[super touchesBegan: touches withEvent: event];
}
//獲取單詞的范圍
- (NSRange)getWordRange:(NSInteger)characterIndex {
NSInteger left = characterIndex - 1;
NSInteger right = characterIndex + 1;
NSInteger length = 0;
NSString *string = self.attributedText.string;
// 往左遍歷直到空格
while (left >=0) {
NSString *s=[string substringWithRange:NSMakeRange(left, 1)];
if ([self isLetter:s]) {
left --;
} else {
break;
}
}
// 往右遍歷直到空格
while (right < self.text.length) {
NSString *s=[string substringWithRange:NSMakeRange(right, 1)];
if ([self isLetter:s]) {
right ++;
} else {
break;
}
}
// 此時(shí) left 和 right 都指向空格
left ++;
right --;
NSLog(@"letf = %ld, right = %ld",left,right);
length = right - left + 1;
NSRange range = NSMakeRange(left, length);
return range;
}
子類其他代碼:
//判斷是否字母
- (BOOL)isLetter:(NSString *)str {
char letter = [str characterAtIndex:0];
if ((letter >= 'a' && letter <='z') || (letter >= 'A' && letter <= 'Z')) {
return YES;
}
return NO;
}
//修改屬性字符串
- (void)modifyAttributeInRange:(NSRange)range {
NSString *string = self.attributedText.string;
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];
//添加文字顏色
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
//添加文字背景顏色
[attString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:range];
self.attributedText = attString;
}
測(cè)試代碼:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200);
WordTextView *textView = [[WordTextView alloc]initWithFrame:frame];
textView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:textView];
NSString *string = @"The UIFont class provides the interface for getting and setting font information. The class provides you with access to the font’s characteristics and also provides the system with access to the font’s glyph information, which is used during layout. You use font objects by passing them to methods that accept them as a parameter.";
NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];
textView.attributedText = attrStr;
}
除了通過(guò)繼承實(shí)現(xiàn),也可以給 textView 添加手勢(shì)來(lái)實(shí)現(xiàn)。
如果有更好的方法宣赔,求分享预麸。
<p>