2014-10-05 14 views
18

Mam dziwny problem z Xcode 6.1 GM.Swift - Tekst rysunkowy z drawInRect: withAttributes:

let text: NSString = "A" 
let font = NSFont(name: "Helvetica Bold", size: 14.0) 

let textRect: NSRect = NSMakeRect(5, 3, 125, 18) 
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle 
textStyle.alignment = NSTextAlignment.LeftTextAlignment 
let textColor = NSColor(calibratedRed: 0.147, green: 0.222, blue: 0.162, alpha: 1.0) 

let textFontAttributes = [ 
    NSFontAttributeName: font, 
    NSForegroundColorAttributeName: textColor, 
    NSParagraphStyleAttributeName: textStyle 
] 

text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes) 

Błąd jest zgodne niech texFontAttributes ...

Cannot convert the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible' 

Kod ten jest doskonale pracował aż Xcode 6.1 GM.

Kiedy jestem próbował oświadczyć textFontAttributes jak komunikat o błędzie NSDictionary zostaje zmieniony na:

Cannot convert the expression's type 'NSDictionary' to type 'NSString!' 

nie mam pojęcia, jak rozwiązać ten problem :(

+0

Nie wiem dlaczego, ale 'drawAtPoint: withAttributes:' 'drawInRect: withAttributes:' 'drawWithRect: opcje: atrybuty:' ' sizeWithAttributes: 'i' boundingRectWithSize: options: attributes: 'are" Niedostępne w Swift " – JDS

+0

@JDS Jest niedostępne nie w Swift, ale w Swift's * String * type. Możesz wywołać te metody na * NSString *, podobnie jak OP. – Blaszard

Odpowiedz

22

problemem jest to, że font jest opcjonalny, ponieważ contructors wygoda teraz zwracać wartości opcjonalne, s O font należy odwinąć się wartość w słowniku:

if let actualFont = font { 
    let textFontAttributes = [ 
     NSFontAttributeName: actualFont, 
     NSForegroundColorAttributeName: textColor, 
     NSParagraphStyleAttributeName: textStyle 
    ] 

    text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes) 
} 
0

mam ten kawałek kodu w moim aplikacja, która działa bez problemów:

var textAttributes: [String: AnyObject] = [ 
     NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 1.0).CGColor, 
     NSFontAttributeName : UIFont.systemFontOfSize(17) 
    ] 
+2

W systemie iOS 8 przy użyciu .drawAtPoint: withAttributes, że .CGColor powoduje awarię. Oczekuje UIColor. – bitmusher

+0

@bitmusher ty, moja aplikacja uległa awarii z tajemniczych powodów, a twój komentarz doprowadził mnie do właściwej przyczyny. UIColor, a nie CGColor. Można by pomyśleć, że kompilator może to złapać, ale te atrybuty spakowane w tablicę łańcuchów są trochę niebezpieczne, pochodzące od Swifta, który jest bardzo wybredny i wyraźny. – DrZ214

1

Jest też inna opcja.

let textFontAttributes = [ 
    NSFontAttributeName : font!, 
    NSForegroundColorAttributeName: textColor, 
    NSParagraphStyleAttributeName: textStyle 
] 
0

W Swift 4

let attributeDict: [NSAttributedStringKey : Any] = [ 
     .font: font!, 
     .foregroundColor: textColor, 
     .paragraphStyle: textStyle, 
    ] 

    text.draw(in: rect, withAttributes: attributeDict)