【问题标题】:Remove bulletpoints from NSMutableAttributedString从 NSMutableAttributedString 中删除项目符号
【发布时间】:2018-07-13 14:09:38
【问题描述】:

我正在尝试从 NSMutableAttributedString 中删除 • 项目符号。

我可以使用 .replacingOccurrences(of: text, with: "") 删除文本“单击以打开图像...”,但无论我做什么,项目符号点都不会消失。无论如何,我认为这不是正确的解决方案。

我在标签的属性文本上调用下面的扩展 例如:

label.attributedText = someString.htmlAttributedString()

有什么建议吗?

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 字符串如下所示:

{
    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;
}

这就是它的样子

.replacingOccurrences(of: "•", with: "")

.replacingOccurrences(of: "点击打开图片!", with: "")

.replacingOccurrences(of: "Maresteimagina", with: "")

【问题讨论】:

  • 能给个html字符串吗?复制可能很有用。或者至少你能打印出属性字符串吗?它也可能有用。
  • 我用 json 文件和属性字符串编辑了我的帖子
  • 使用.replacingOccurrences(of: "•", with: "")有什么问题
  • 它不工作,我不知道为什么。它适用于除要点之外的所有内容。
  • 你想要这个: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: "")}

标签: swift string uilabel nsattributedstring


【解决方案1】:

在您当前的代码中,我完全不知道应该做什么:

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)
    }
}

如果formattedStringattributedString 短,您使用NSRange(0..&lt;attributedString.length) 会导致崩溃。这是一个要点,所以你不应该有这个问题,但仍然存在。 而它背后的目标却是我无法企及的。

子弹\u{2022}是解析后创建的,所以需要解析formattedString.string,可以怎么做:

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: "")}

我不明白您是否只想删除项目符号或文本的其余部分(其他人是否尝试使用.replacingOccurrences(of: "Click to open image!", with: "") 只是为了查看您确实能够到达该行并删除子字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2023-03-12
    • 2014-07-02
    • 2014-10-29
    • 2015-11-07
    • 1970-01-01
    • 2023-03-03
    • 2018-12-11
    相关资源
    最近更新 更多