我使用UIMenuItem
和UIMenuController
到亮點功能添加到我的UITextView
,因此用戶可以更改所選文本的背景顏色,如圖片所示波紋管:UITextView的文本選擇和突出的iOS跳8
-
在
- Setected文本與提供給用戶的亮點特點:
UITextView
- 突出顯示的文本中
UITextView
用新的背景顏色,在高亮特徵輕敲後由用戶選擇:
在的iOS 7以下代碼工作完美地完成這項任務:
- (void)viewDidLoad {
[super viewDidLoad];
UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(highlight)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:highlightMenuItem]];
}
- (void)highlight {
NSRange selectedTextRange = self.textView.selectedRange;
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor redColor]
range:selectedTextRange];
// iOS 7 fix, NOT working in iOS 8
self.textView.scrollEnabled = NO;
self.textView.attributedText = attributedString;
self.textView.scrollEnabled = YES;
}
但在iOS 8文本選擇正在跳躍。當我使用來自UIMenuItem
和UIMenuController
的突出顯示功能時,它也會跳轉到另一個UITextView
偏移量。
我該如何解決這個問題iOS 8?
非常感謝你!我試圖解決這個問題:textView.scrollEnabled = false ... textView.scrollEnabled = true,但在ios 8中不起作用。剛剛添加self.textView.layoutManager.allowsNonContiguousLayout = false並且一切正常 – schirrmacher 2014-11-23 18:33:23
好極了。我很高興能夠提供幫助。 – Winston 2014-11-24 01:46:03
在我的應用程序中,禁用scrollEnabled仍然阻止了iOS 8中的跳轉,但是任何嘗試重新啓用它都沒有影響,因此在第一次編輯後滾動變爲永久禁用。禁用allowedNonContiguousLayout可以避免這個問題。我不知道這樣做是否有缺點,所以爲了以防萬一,編輯後我重新啓用了它。 – arlomedia 2015-03-12 21:33:40