Friday, 15 June 2012

iphone - UITextView text selection and highlight jumping in iOS 8 -



iphone - UITextView text selection and highlight jumping in iOS 8 -

i'm using uimenuitem , uimenucontroller add together highlight feature uitextview, user can alter background color of selected text, shown in pictures bellow:

setected text in uitextview highlight feature available user:

highlighted text in uitextview new background color, chosen user after tapping on highlight feature:

in ios 7 next code is working accomplish task:

- (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; }

but in ios 8 text selection jumping. when utilize highlight feature uimenuitem , uimenucontroller jumps uitextview offset.

how solve problem in ios 8?

i ended solving problem this, , if else has more elegant solution please allow me know:

- (void)viewdidload { [super viewdidload]; uimenuitem *highlightmenuitem = [[uimenuitem alloc] initwithtitle:@"highlight" action:@selector(highlight)]; [[uimenucontroller sharedmenucontroller] setmenuitems:[nsarray arraywithobject:highlightmenuitem]]; float sysver = [[[uidevice currentdevice] systemversion] floatvalue]; if (sysver >= 8.0) { self.textview.layoutmanager.allowsnoncontiguouslayout = no; } } - (void)highlight { nsrange selectedtextrange = self.textview.selectedrange; [attributedstring addattribute:nsbackgroundcolorattributename value:[uicolor redcolor] range:selectedtextrange]; float sysver = [[[uidevice currentdevice] systemversion] floatvalue]; if (sysver < 8.0) { // ios 7 prepare self.textview.scrollenabled = no; self.textview.attributedtext = attributedstring; self.textview.scrollenabled = yes; } else { self.textview.attributedtext = attributedstring; } }

ios iphone ios7 ios8 uitextview

No comments:

Post a Comment