Swift 4 attributedString get typing attributes

前端 未结 3 1182
轻奢々
轻奢々 2021-01-05 01:52

I am trying to create AttributedString and add the attributes from

typingAttributes(from textView)

The problem is that

.typingAt

3条回答
  •  天命终不由人
    2021-01-05 02:15

    Here is my helper class, which I use custom fonts

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

提交回复
热议问题