Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 从NSMutableAttributeString中删除bulletpoints_Swift_String_Uilabel_Nsattributedstring - Fatal编程技术网

Swift 从NSMutableAttributeString中删除bulletpoints

Swift 从NSMutableAttributeString中删除bulletpoints,swift,string,uilabel,nsattributedstring,Swift,String,Uilabel,Nsattributedstring,我正在尝试从NSMutableAttributedString中删除•bulletpoints 我可以用.replacingOccurrences(of:text,with:)删除文本“单击打开图像…”,但无论我做什么,项目符号都不会消失。无论如何,我认为这不是正确的解决方案 我正在调用标签的AttributeText上的以下分机 例: 有什么建议吗 extension String { func htmlAttributedString() -> NSMutableAttrib

我正在尝试从NSMutableAttributedString中删除•bulletpoints

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

我正在调用标签的AttributeText上的以下分机 例:

有什么建议吗

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
    }
}
扩展字符串{
func HtmlatAttributedString()->NSMutableAttributedString{
guard let data=self.data(使用:String.Encoding.utf8,allowossyconversion:false)else{return NSMutableAttributedString()}
guard let格式化字符串=
试试?NSMutableAttributeString(
数据:数据,
选项:[
.documentType:nsAttributeString.documentType.html,
.characterEncoding:String.Encoding.utf8.rawValue],
文件属性:无)
else{return NSMutableAttributedString()}
让bulletPoint:String=“\u{2022}”
让attributedString=NSMutableAttributedString(字符串:bulletPoint)
formattedString.enumerateAttributes(在:NSRange(0

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;
}
{
NSColor=“kCGColorSpaceModelRGB 0 1”;
NSFont=“font-family:\“Times New Roman\”字体重量:正常;字体样式:正常;字体大小:12.00pt”;
NSKern=0;
NSParagraphStyle=“对齐3,行距0,段落间距12,段落间距0之前,头缩进0,尾缩进0,第一行头缩进0,线宽15/0,线宽倍数0,换行模式0,制表符(\n),默认TabInterval 36,块(\n),列表(\n),基写方向0,连字符系数0,拧紧位置号,头级别0”;
NSStrokeColor=“kCGColorSpaceModelRGB 0 1”;
NSStrokeWidth=0;
}   •   {
NSColor=“kCGColorSpaceModelRGB 0 0.933333 1”;
NSFont=“font-family:\”TimesNewRomanPS-BoldMT\“字体大小:粗体;字体样式:普通;字体大小:12.00pt”;
NSParagraphStyle=“对齐0、行距0、段落间距0、段落间距0、段落间距0、首缩进36、尾缩进0、首行首缩进0、线宽15/0、线宽倍数0、换行模式0、制表符(\n 11L、\n 36N\n)、默认TabInterval 36、块(null)、列表(\n \“NSTextList 0x60c000442160格式\”\n),BaseWritingDirection 0,连字符系数0,拧紧位置号,表头级别0”;
}点击打开图片{
NSColor=“kCGColorSpaceModelRGB 0 0.933333 1”;
NSFont=“font-family:\”TimesNewRomanPS-BoldMT\“字体大小:粗体;字体样式:普通;字体大小:12.00pt”;
NSKern=0;
NSLink=“应用程序lewebdata://AC01F520-17C4-4F1A-BB81-0A54CBE9C2F6/images/galerie/fotoreportaj-faleza-inferioara-galati/01.jpg";
NSParagraphStyle=“对齐0、行距0、段落间距0、段落间距0、段落间距0、首缩进36、尾缩进0、首行首缩进0、线宽15/0、线宽倍数0、换行模式0、制表符(\n 11L、\n 36N\n)、默认TabInterval 36、块(null)、列表(\n \“NSTextList 0x60c000442160格式\”\n),BaseWritingDirection 0,连字符系数0,拧紧位置号,表头级别0”;
NSStrokeColor=“kCGColorSpaceModelRGB 0 0.933333 1”;
NSStrokeWidth=0;
NSUnderline=1;
}马雷斯特幻想酒店{
NSColor=“kCGColorSpaceModelRGB 0 0.933333 1”;
NSFont=“font-family:\“Times New Roman\”字体重量:正常;字体样式:正常;字体大小:12.00pt”;
NSKern=0;
NSLink=“应用程序lewebdata://AC01F520-17C4-4F1A-BB81-0A54CBE9C2F6/images/galerie/fotoreportaj-faleza-inferioara-galati/01.jpg";
NSParagraphStyle=“对齐0、行距0、段落间距0、段落间距0、段落间距0、首缩进36、尾缩进0、首行首缩进0、线宽15/0、线宽倍数0、换行模式0、制表符(\n 11L、\n 36N\n)、默认TabInterval 36、块(null)、列表(\n \“NSTextList 0x60c000442160格式\”\n),BaseWritingDirection 0,连字符系数0,拧紧位置号,表头级别0”;
NSStrokeColor=“kCGColorSpaceModelRGB 0 0.933333 1”;
NSStrokeWidth=0;
NSUnderline=1;
}
这就是它的外观

.重复发生(指:“•”和:”)

.replacingOccurrences(of:“单击打开图像!”,带:“”)

.重复发生(关于“Mareste imaginea”,带:“”)


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

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

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

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

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

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

你能给出html字符串吗?它可能有助于复制。或者至少你能打印attributedString吗?它也可能有用。我用json文件和AttributedStrings编辑了我的文章使用
有什么问题。replacingOccurrences(of:“•”,with:”)
它不起作用,我不知道为什么。它对除了bulletpoint之外的所有东西都起作用。您希望这样:
let regex=try!NSRegularExpression(模式:\\s\u{2022}\\s,选项:[]);let matches=regex.matches(in:formattedString.string,options:[],range:NSMakeRange(0,formattedString.string.utf16.count));matches.reversed().forEach{formattedString.replaceCharacters(in:$0.range,with:“)}
?您能给出html字符串吗?它可能有助于复制。或者至少您能打印AttributeString吗?它也可能有用。例如
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: "")}