When I'm trying to add attributes to attributed string that contains emojis sometimes some of emojis are broken. For regular text works perfectly. Any ideas what am I doing wrong?
Here is my function - it should bold quotations (text between first and last quotation mark)
func boldQuotation(str: String, fontSize: CGFloat) -> NSAttributedString {
let normalAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(fontSize)]
let boldAttributes = [NSFontAttributeName : UIFont.boldSystemFontOfSize(fontSize)]
let attributedStr = NSMutableAttributedString(string: str, attributes: normalAttributes)
let firstQuotationMarkRange = str.rangeOfString("\"")
let lastQuotationMarkRange = str.rangeOfString("\"", options: [.BackwardsSearch], range: nil, locale: nil)
guard let firstIndex = firstQuotationMarkRange?.startIndex, lastIndex = lastQuotationMarkRange?.endIndex else {
return attributedStr
}
attributedStr.addAttributes(boldAttributes, range: NSMakeRange(str.startIndex.distanceTo(firstIndex), firstIndex.distanceTo(lastIndex)))
return attributedStr
}
Here is sample of text that cannot be correctly attributed:
let str = "\"\""
The result looks like this:
I'm using Swift 2.3, iOS 10.2.1 , Xcode 8.2.1, Deployment target: 9.3