ios - Mutable string color change alters character to be displayed as square -


i changing color of last character of table view section title , getting odd result ios 9+, swift 3.

the last character checkmark ✔︎ , color green. result green square instead of green checkmark. if print console checkmark shows correctly. if remove color change, shows checkmark fine (in black). if use symbol such double exclamation point, works fine.

simplified code

struct personconstants{     static let default_status_index : int = 1     static let validstatus : nsarray = ["✔︎","?","‼"] }  let colorsarray = [     uicolor(red: 29/255.0, green:166/255.0, blue:47/255.0, alpha:1.0),     uicolor(red: 0/255.0, green: 0/255.0, blue: 255/255.0, alpha: 1.0),     uicolor(red: 225/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), ] 

override func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string?{     var title = string(safe.count) + " "     title += nslocalizedstring("accounted", comment: "")     title += ": " + string(personconstants.validstatus[0] as! nsstring)     return title } 

override func tableview(_ tableview: uitableview, willdisplayheaderview view: uiview, forsection section: int) {     if let view = view as? uitableviewheaderfooterview {         view.textlabel!.textcolor = uicolor.black         view.textlabel!.font = uifont.italicsystemfont(ofsize: 17.0)          mymutablestring = nsmutableattributedstring(string: view.textlabel!.text! string)          if sectiontwo.count > 0{             mymutablestring.addattribute(nsforegroundcolorattributename, value: colorsarray[0], range: nsrange(location: (view.textlabel!.text!).characters.count-1,length:1))             view.textlabel!.attributedtext = mymutablestring             //print(mymutablestring.string)         }else{             view.textlabel!.text = ""         }     } } 

bad result

enter image description here

that happens because nsmutableattributedstring works utf-16 based offset , count, need pass utf-16 based nsrange attributes of nsmutableattributedstring.

it's not simple task in swift 3...

let text = view.textlabel!.text ?? "" let lastcharindex = text.index(before: text.endindex) let lastcharutf16index = lastcharindex.sameposition(in: text.utf16) let location = text.utf16.startindex.distance(to: lastcharutf16index) let length = lastcharutf16index.distance(to: text.utf16.endindex) mymutablestring.addattribute(nsforegroundcolorattributename, value: colorsarray[0],                              range: nsrange(location: location, length: length)) 

by way, if declare validstatus as:

static let validstatus: [string] = ["✔︎","?","‼"] 

you can make other parts of code simpler:

title += ": " + personconstants.validstatus[0] 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -