Xcode 6.1 GM有一個奇怪的問題。Swift - 使用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)
錯誤是本着讓texFontAttributes ...
Cannot convert the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible'
此代碼完美地工作,直到Xcode的6.1 GM。
當我試圖聲明textFontAttributes作爲NSDictionary的錯誤信息更改爲:
Cannot convert the expression's type 'NSDictionary' to type 'NSString!'
我不知道如何解決這個問題:(
我不知道爲什麼,但'drawAtPoint:withAttributes:','drawInRect:withAttributes:','drawWithRect:選項:屬性:',' sizeWithAttributes:'和'boundingRectWithSize:options:attributes:'are「在Swift中不可用」 – JDS 2014-10-05 14:05:47
@JDS它在Swift中不可用,但Swift的* String *類型不可用。你可以在* NSString *上調用這些方法,就像OP一樣。 – Blaszard 2015-03-06 12:59:48