【问题标题】:I'm unable to enumerate an NSAttributed Text in Swift 3我无法在 Swift 3 中枚举 NSAttributed Text
【发布时间】:2016-10-21 23:02:02
【问题描述】:

我无法让它在 swift 3 中正确编译。我遇到了

的问题

此处为实际代码。我不知道为什么它在代码中要求额外的

 data2 = items.data(using: .utf8)
            attrString = NSAttributedString(htmlData:data2!,options:[DTDefaultFontSize:13.0,DTDefaultFontFamily:"Verdana",DTDefaultFirstLineHeadIndent:5.0],documentAttributes:nil)
            print(attrString)

            attrString?.enumerateAttribute(NSAttachmentAttributeName , in: NSMakeRange(0, (attrString?.length)!), options: 0, using:^(id value,NSRange range,BOOL *test){
                if(value){
                    print(value)
                }
                })

【问题讨论】:

    标签: ios swift nsattributedstring enumerate


    【解决方案1】:

    您必须复制了 Objective-C 示例,但未将其完全转换为 swift。块语法和 if(value) 之类的东西是有效的Objective-C,但不是有效的swift。以下代码在Swift-playground 中正常工作:

    let attrString = NSAttributedString(string: "test", attributes: [NSForegroundColorAttributeName : UIColor.red, NSUnderlineColorAttributeName : UIColor.green])
    attrString.enumerateAttribute(NSForegroundColorAttributeName , in: NSMakeRange(0, attrString.length), options: [.longestEffectiveRangeNotRequired]) { value, range, isStop in
        if let value = value {
            print(value)
        }
    }
    

    【讨论】:

    • 非常令人沮丧的是,xCode 中的自动完成功能无法显示。
    • 我只想补充一点,对于更长的 textViews,这会非常慢。在具有包含 3598 个单词且没有 过多 格式的 textView 的现代 iPhone XR 上运行此操作最多可能需要 7 秒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多