Wenn ich Attribute attributierte Zeichenfolge, die Emojis enthält, hinzufügen möchte, sind manchmal einige der Emojis gebrochen. Für normalen Text funktioniert perfekt. Irgendwelche Ideen was mache ich falsch?Hinzufügen von Attributen zu einer Zeichenfolge mit Emojis bricht einige Emojis
Hier ist meine Funktion - es sollte fett Zitate (Text zwischen dem ersten und letzten Anführungszeichen)
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
}
Hier Probe von Text, der nicht korrekt zugeordnet werden:
let str = "\"\""
Das Ergebnis sieht wie folgt aus:
Ich benutze Swift 2.3, iOS 10.2.1, Xcode 8.2.1, Bereitstellungsziel: 9.3