2014-10-05 12 views
18

Ho strani problemi con Xcode 6.1 GM.Swift - Disegno di testo con 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) 

errore è in linea lascia texFontAttributes ...

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

Questo codice è funzionato perfettamente fino a quando Xcode 6.1 GM.

Quando sto cercato di dichiarare textFontAttributes come messaggio di errore NSDictionary viene modificato in:

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

non ho idea di come risolvere questo problema :(

+0

Non so perché, ma 'drawAtPoint: withAttributes:', 'drawInRect: withAttributes:', 'drawWithRect: opzioni: attributi:', ' sizeWithAttributes: 'e' boundingRectWithSize: options: attributes: 'sono" Non disponibile in Swift " – JDS

+0

@JDS Non è disponibile non in Swift ma Swift's * String * type. Puoi chiamare questi metodi su * NSString * come ha fatto l'OP. – Blaszard

risposta

22

Il problema è che font è facoltativo, perché le Case costruttrici di convenienza ora restituiscono valori opzionali, s o font ha bisogno di essere scartato per essere un valore nel dizionario:

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

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

ho questo pezzo di codice nel mio app che funziona senza problemi:

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

In iOS 8, utilizzando .drawAtPoint: withAttributes, che .CGColor causa un arresto anomalo. Si aspetta un UIColor. – bitmusher

+0

@bitmusher ty, la mia app si è bloccata per motivi misteriosi e il tuo commento mi ha portato alla giusta causa. UIColor, non CGColor. Si potrebbe pensare che il compilatore possa prenderlo, ma quegli attributi racchiusi in una serie di stringhe sono un po 'pericolosi provenienti da Swift, che è altrimenti molto schizzinoso ed esplicito. – DrZ214

1

Questa è anche un'altra opzione.

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

In Swift 4

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

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