2017-09-28 17 views
7

Próbuję utworzyć AttributedString i dodać atrybuty zSwift 4 attributedString get wpisując atrybuty

typingAttributes(from textView)

Problemem jest to, że

.typingAttributes

powrót

[String, Any]

i

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

potrzeby

[NSAttributedStringKey: Any]

Mój kod:

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

Nie chcę, aby utworzyć w cyklu przejść przez wszystkie klucze i zmieniać je do

NSAttributedStringKey 
+0

Zastanawiam * dlaczego * klawisze w typingAttributes słowników do strun i nie NSAttributedStringKey. Możesz złożyć raport o błędzie. –

+0

Będę, ale na razie chcę, żeby to zadziałało. –

+0

Wygląda na to, że istnieją dwa otwarte radary adresujące warianty tego: https://openradar.appspot.com/34994725 i https://openradar.appspot.com/34402659 – Aaron

Odpowiedz

7

Można mapować słownika [String: Any] do słownika [NSAttributedStringKey: Any] z

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

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

Oto możliwy sposób przedłużenie do tego celu, jest ograniczone do słowników z kluczami tekstowymi:

extension Dictionary where Key == String { 

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

Myślę, że jeszcze lepsze rozwiązanie. Stworzyłem rozszerzenie.

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

Dobry pomysł. - Jeśli ograniczysz rozszerzenie do kluczy ciągów, opcjonalny rzut nie będzie potrzebny. Dodałem prostszą wersję do mojej odpowiedzi. –

0

Oto moja klasa pomocnika, którego używam niestandardowe czcionki

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