Reputation: 1225
I'm trying to remove the • bulletpoints from an NSMutableAttributedString.
I can delete the text "Click to open image ..." with .replacingOccurrences(of: text, with: "") but the bullet points will not go away no matter what I do. I don't think this is the right solution anyway.
I am calling the extension below on a label's attributedText ex:
label.attributedText = someString.htmlAttributedString()
Any suggestions ?
extension String {
func htmlAttributedString() -> NSMutableAttributedString {
guard let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false) else { return NSMutableAttributedString() }
guard let formattedString =
try? NSMutableAttributedString(
data: data,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
else { return NSMutableAttributedString() }
let bulletPoint: String = "\u{2022}"
let attributedString = NSMutableAttributedString(string: bulletPoint)
formattedString.enumerateAttributes(in: NSRange(0..<attributedString.length), options: []) { (attributes, range, _) -> Void in
for (attribute, _) in attributes {
formattedString.removeAttribute(attribute, range: range)
}
}
formattedString.trimCharactersInSet(charSet: NSCharacterSet.whitespacesAndNewlines as NSCharacterSet)
return formattedString
}
}
html string looks like this:
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fb06e91edb0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 3, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
} • {
NSColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSFont = "<UICTFont: 0x7fb06e93e600> font-family: \"TimesNewRomanPS-BoldMT\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 36, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 11L,\n 36N\n), DefaultTabInterval 36, Blocks (null), Lists (\n \"NSTextList 0x60c000442160 format <{disc}>\"\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}Click to open image!{
NSColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSFont = "<UICTFont: 0x7fb06e93e600> font-family: \"TimesNewRomanPS-BoldMT\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSLink = "applewebdata://AC01F520-17C4-4F1A-BB81-0A54CBE9C2F6/images/galerie/fotoreportaj-faleza-inferioara-galati/01.jpg";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 36, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 11L,\n 36N\n), DefaultTabInterval 36, Blocks (null), Lists (\n \"NSTextList 0x60c000442160 format <{disc}>\"\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSStrokeWidth = 0;
NSUnderline = 1;
} Mareste imaginea {
NSColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSFont = "<UICTFont: 0x7fb06e91edb0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSLink = "applewebdata://AC01F520-17C4-4F1A-BB81-0A54CBE9C2F6/images/galerie/fotoreportaj-faleza-inferioara-galati/01.jpg";
NSParagraphStyle = "Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 36, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 11L,\n 36N\n), DefaultTabInterval 36, Blocks (null), Lists (\n \"NSTextList 0x60c000442160 format <{disc}>\"\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSStrokeWidth = 0;
NSUnderline = 1;
}
This is how it looks with
.replacingOccurrences(of: "•", with: "")
.replacingOccurrences(of: "Click to open image!", with: "")
.replacingOccurrences(of: "Mareste imaginea", with: "")
Upvotes: 0
Views: 829
Reputation: 26086
In your current code, I have absolutely no idea of what is that supposed to do:
let bulletPoint: String = "\u{2022}"
let attributedString = NSMutableAttributedString(string: bulletPoint)
formattedString.enumerateAttributes(in: NSRange(0..<attributedString.length), options: []) { (attributes, range, _) -> Void in
for (attribute, _) in attributes {
formattedString.removeAttribute(attribute, range: range)
}
}
The fact that you use NSRange(0..<attributedString.length)
, should cause a crash if formattedString
is shorter than attributedString
. That's a bullet point, so you shouldn't have the issue, but still.
And the goal behind it escapes my reach.
The bullet •
or \u{2022}
is created after the parsing, so you need to parse the formattedString.string
, what you could do:
let regex = try! NSRegularExpression(pattern: "\\s\u{2022}\\s", options: [])
let matches = regex.matches(in: formattedString.string, options: [], range: NSMakeRange(0, formattedString.string.utf16.count))
matches.reversed().forEach { formattedString.replaceCharacters(in: $0.range, with: "")}
I didn't understood if you wanted to remove only the bullets or the rest of the text (was the others tries with .replacingOccurrences(of: "Click to open image!", with: "")
just to see that you were indeed able to reach that line and remove the substrings or not.
Upvotes: 2