【问题标题】:Swift 4 attributedString get typing attributesSwift 4 属性字符串获取输入属性
【发布时间】:2018-03-10 06:53:45
【问题描述】:

我正在尝试创建 AttributedString 并添加来自

的属性

typingAttributes(from textView)

问题是

.typingAttributes

返回

[String, Any]

NSAttributedString(string:.. , attributes:[])

需要

[NSAttributedStringKey: Any]

我的代码:

NSAttributedString(string: "test123", attributes: self.textView.typingAttributes)

我不想在循环中创建 for 来遍历所有键并将它们更改为

NSAttributedStringKey

【问题讨论】:

  • 我想知道为什么 typingAttributes 字典中的键是字符串而不是 NSAttributedStringKey。您可能需要提交错误报告。
  • 我会,但现在我想让它工作
  • 看起来有两个开放雷达可以解决这个问题:openradar.appspot.com/34994725openradar.appspot.com/34402659
  • 我一个月前报告了这个错误,但苹果仍然没有回应。别担心。

标签: swift ios11 swift4 xcode9


【解决方案1】:

您可以将[String: Any] 字典映射到 [NSAttributedStringKey: Any]字典与

let typingAttributes = Dictionary(uniqueKeysWithValues: self.textView.typingAttributes.map {
    key, value in (NSAttributedStringKey(key), value)
})

let text = NSAttributedString(string: "test123", attributes: typingAttributes)

这是一种可能的扩展方法,它是 仅限于带有字符串键的字典:

extension Dictionary where Key == String {

    func toAttributedStringKeys() -> [NSAttributedStringKey: Value] {
        return Dictionary<NSAttributedStringKey, Value>(uniqueKeysWithValues: map {
            key, value in (NSAttributedStringKey(key), value)
        })
    }
}

【讨论】:

    【解决方案2】:

    我认为更好的解决方案。我创建了扩展。

    public extension Dictionary {
        func toNSAttributedStringKeys() -> [NSAttributedStringKey: Any] {
            var atts = [NSAttributedStringKey: Any]()
    
            for key in keys {
                if let keyString = key as? String {
                    atts[NSAttributedStringKey(keyString)] = self[key]
                }
            }
    
            return atts
        }
    }
    

    https://gist.github.com/AltiAntonov/f0f86e7cd04c61118e13f753191b5d9e

    【讨论】:

    • 好主意。 – 如果您将扩展限制为字符串键,则不需要可选转换。我在答案中添加了一个更简单的版本。
    【解决方案3】:

    这是我的辅助类,我使用自定义字体

    import UIKit
    
    struct AttributedStringHelper {
    
    enum FontType: String {
        case bold = "GothamRounded-Bold"
        case medium = "GothamRounded-Medium"
        case book = "GothamRounded-Book"
    }
    
    static func getString(text: String, fontType: FontType, size: CGFloat, color: UIColor, isUnderlined: Bool? = nil) -> NSAttributedString {
    
        var attributes : [NSAttributedStringKey : Any] = [
            NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont(name: fontType.rawValue, size: size)!,
            NSAttributedStringKey.foregroundColor : color]
    
        if let isUnderlined = isUnderlined, isUnderlined {
            attributes[NSAttributedStringKey.underlineStyle] = 1
        }
    
        let attributedString = NSAttributedString(string: text, attributes: attributes)
        return attributedString
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 2012-10-30
      • 1970-01-01
      相关资源
      最近更新 更多