2017-09-28 16 views
7

मैं AttributedString बना सकते हैं और सेस्विफ्ट 4 attributedString प्राप्त टाइपिंग विशेषताओं

typingAttributes(from textView)

विशेषताओं को जोड़ने की कोशिश कर रहा हूँ समस्या यह है कि

.typingAttributes

वापसी

[String, Any]

और

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

जरूरतों

[NSAttributedStringKey: Any]

मेरे कोड:

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

मैं चक्र में सभी चाबियाँ के माध्यम से जाने के लिए और उन्हें बदलने के लिए बनाने के लिए नहीं करना चाहते हैं

NSAttributedStringKey 
+0

मुझे आश्चर्य है कि * का उपयोग typingAttributes में चाबियाँ एक तार क्यों * शब्दकोशों और नहीं NSAttributedStringKey है। आप एक बग रिपोर्ट दर्ज करना चाह सकते हैं। –

+0

मैं अभी भी इसे काम करना चाहता हूं –

+0

ऐसा लगता है कि इसमें दो खुले रडार हैं जो इस प्रकार के बदलावों को संबोधित करते हैं: https://openradar.appspot.com/34994725 और https://openradar.appspot.com/34402659 – Aaron

उत्तर

7

आप

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

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

यहाँ के साथ एक [NSAttributedStringKey: Any] शब्दकोश में [String: Any] शब्दकोश मैप कर सकते हैं उस उद्देश्य के लिए एक संभावित विस्तार विधि, यह शब्दकोशों के लिए प्रतिबंधित है है स्ट्रिंग कुंजी के साथ:

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

+0

अच्छा विचार। - यदि आप एक्सटेंशन को स्ट्रिंग कुंजियों तक सीमित करते हैं तो वैकल्पिक कास्ट की आवश्यकता नहीं होती है। मैंने अपने जवाब में एक सरल संस्करण जोड़ा है। –

0

यहाँ मेरी सहायक वर्ग है, जो मैं कस्टम फोंट

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 
} 
} 
संबंधित मुद्दे