ios - How to make part of UITextView uneditable and clickable, with properly shown emojis? -
i have uitextview
in app, , should contain uneditable , clickable suffix (categorystring), facebook 'feeling, watching, eating,...' status. shouldchangetextinrange
, textviewdidchange
methods this:
func textviewdidchange(textview: uitextview) { dispatch_async(dispatch_get_main_queue(), { textview.selectedrange = nsmakerange(self.globalint, 0) //on long press backspace, goes wrong //nsmutableattributedstring, clear textview //and set it's text categorystring again: if(!textview.text.hassuffix(self.categorystring)){ self.globalint = 0 self.posttextview.font = constants.fonts.posttextcategory textview.text = self.categorystring textview.selectedrange = nsmakerange(0,0) } }) self.mymutablestring = nsmutableattributedstring(string: self.posttextview.text, attributes: [nsfontattributename:constants.fonts.posttextcategory!]) self.mymutablestring.addattribute(nsfontattributename, value: constants.fonts.posttext!, range: nsrange(location: 0, length: maxglobalint)) self.posttextview.attributedtext = self.mymutablestring } func textview(textview: uitextview, shouldchangetextinrange range: nsrange, replacementtext text: string) -> bool { var temptext : nsstring = text var currenttext : nsstring = textview.text var texttoreturn = currenttext.length + (temptext.length - range.length) if(text == "\n"){ textview.resignfirstresponder() } var signaturelength = count(categorystring) globalint = texttoreturn - signaturelength maxglobalint = texttoreturn - signaturelength if(range.location > texttoreturn - signaturelength){ return false }else{ return texttoreturn <= 160 + signaturelength } }
uitextview
font bold default, use nsmutableattributedstring
change font editable part of uitextview
(you can see in textviewdidchange
).
first problem: when edit uitextview
, , want set cursor somewhere in middle of text, can type 1 character (even backspace) , cursor automatically move ending position of editable part of text. if add these lines of code shouldchangetextinrange
dispatch_async(dispatch_get_main_queue(), { self.globalint = range.location + 1 if(count(text) == 0){ self.globalint = self.globalint - 1 }else{ self.globalint = range.location + 1 } })
that solves problem, cursor position ok, have new 1 - emoji characters don't show properly:
i assume that's happening because emoji character length, don't have idea how solve this?
second problem: how make uneditable part of uitextview
clickable? (instead of setting cursor there)
Comments
Post a Comment