diff --git a/Tusker/Extensions/AttributedString+Trim.swift b/Tusker/Extensions/AttributedString+Trim.swift index 60ddeea2ef..e49ea31096 100644 --- a/Tusker/Extensions/AttributedString+Trim.swift +++ b/Tusker/Extensions/AttributedString+Trim.swift @@ -10,13 +10,13 @@ import Foundation extension NSMutableAttributedString { - func trimCharactersInSet(_ charSet: CharacterSet) { + func trimTrailingCharactersInSet(_ charSet: CharacterSet) { var range = (string as NSString).rangeOfCharacter(from: charSet) - while range.length != 0 && range.location == 0 { - replaceCharacters(in: range, with: "") - range = (string as NSString).rangeOfCharacter(from: charSet) - } +// while range.length != 0 && range.location == 0 { +// replaceCharacters(in: range, with: "") +// range = (string as NSString).rangeOfCharacter(from: charSet) +// } range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards) while range.length != 0 && range.length + range.location == length { diff --git a/Tusker/Views/ContentLabel.swift b/Tusker/Views/ContentLabel.swift index cb5b072b9d..62e4f6654c 100644 --- a/Tusker/Views/ContentLabel.swift +++ b/Tusker/Views/ContentLabel.swift @@ -28,7 +28,12 @@ class ContentLabel: TTTAttributedLabel { let (attributedText, links) = attributedTextForHTMLNode(body) let mutAttrString = NSMutableAttributedString(attributedString: attributedText) - mutAttrString.trimCharactersInSet(.whitespacesAndNewlines) + + // only trailing whitespace can be trimmed here + // when posting an attachment without any text, pleromafe includes U+200B ZERO WIDTH SPACE at the beginning + // this would get trimmed and cause range out of bounds crashes + mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines) + mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length)) self.text = mutAttrString for (range, url) in links {