I am trying to make the selected text in a UITextview bold or italic and this works for me, but let's say I got a string "one two" and I make "one" bold, so it will look like this: "one two". Now if I want to make "two" bold or italic as well this happens: "one two". Is there any explanation why this happens and is there a fix for this?
let main_string = textView2.text
let string_to_change = selectedTextWBU
let range = (main_string! as NSString).range(of: string_to_change)
let attributedString = NSMutableAttributedString(string: main_string!, attributes: [kCTFontAttributeName as NSAttributedStringKey : font])
if selectedTextWBU == ""{
}else{
//highlight
if sender.tag == 1{
print("SELECTEDTEXTWBU == \(selectedTextWBU)")
textView2.attributedText = attributedString
//italic
}else if sender.tag == 2{
attributedString.addAttribute(kCTFontAttributeName as
NSAttributedStringKey, value: italicsFont, range: range)
textView2.attributedText = attributedString
//bold
}else if sender.tag == 3{
attributedString.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: boldFont, range: range)
textView2.attributedText = attributedString
}
}