我在使用TextView时使用此代码来获取字符串。我在.
之间得到了字符串,但是我需要在iOS中的复制粘贴功能中突出显示选择的抽头字符串。
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[NSTimer scheduledTimerWithTimeInterval:0.001 target:maintextview selector:@selector(resignFirstResponder) userInfo:nil repeats:NO];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
NSRange selectedRange = [textView selectedRange];
NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
NSRange backRange = [backString rangeOfString:@"." options:NSBackwardsSearch];
NSString *forwardString = [maintextview.text substringFromIndex:backRange.location];
NSLog(@"%@",[[forwardString componentsSeparatedByString:@"."] objectAtIndex:1]);
}
点击它时,如何以某种颜色突出显示forwardString
?
答案 0 :(得分:0)
你不能只为forwardString生成一个NSRange(通过使用substringFromIndex而不是substringToIndex)并调用:
[textView setSelectedRange];