2014-10-05 9 views
18

मुझे एक्सकोड 6.1 जीएम के साथ अजीब समस्या है।स्विफ्ट - DrawInRect के साथ टेक्स्ट ड्रॉइंग: एट्रिब्यूट्स:

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 जीएम काम किया है।

जब मैं NSDictionary त्रुटि संदेश के रूप में textFontAttributes घोषित करने की कोशिश कर रहा हूँ करने के लिए बदल जाता है:

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

मैं पता नहीं कैसे इस समस्या को :(

+0

मैं नहीं जानता कि क्यों, लेकिन 'drawAtPoint: withAttributes:' 'drawInRect: withAttributes:' 'drawWithRect: विकल्प: विशेषताएं:' ' sizeWithAttributes: 'और' boundingRectWithSize: विकल्प: विशेषताएं: '" स्विफ्ट में उपलब्ध नहीं हैं " – JDS

+0

@JDS यह स्विफ्ट में नहीं है लेकिन स्विफ्ट के * स्ट्रिंग * प्रकार। ओपी के रूप में आप उन तरीकों को * एनएसएसटींग * पर कॉल कर सकते हैं। – Blaszard

उत्तर

22

समस्या यह है कि font वैकल्पिक है क्योंकि सुविधा contructors अब वैकल्पिक मान है रों ओ अपने शब्दकोश में एक मूल्य होने की unwrapped किए जाने की आवश्यकता font:

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

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

हल करने के लिए मैं में कोड के इस टुकड़े है है मेरी एप्लिकेशन जो समस्याओं के बिना काम करता है:

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

आईओएस 8 में, .drawAtPoint का उपयोग करके: एट्रिब्यूट्स के साथ, वह .CGColor एक क्रैश का कारण बनता है। यह एक यूआईसीओलर की उम्मीद है। – bitmusher

+0

@bitmusher ty, मेरा ऐप रहस्यमय कारणों से क्रैश हो रहा था और आपकी टिप्पणी ने मुझे सही कारण बताया। यूआईसीओलर, CGColor नहीं। आपको लगता है कि कंपाइलर इसे पकड़ सकता है, लेकिन स्ट्रिंग्स की एक सरणी में पैक की गई विशेषताएं स्विफ्ट से थोड़ी खतरनाक होती हैं, जो अन्यथा बहुत ही चुनिंदा और स्पष्ट होती है। – DrZ214

1

यह भी एक और विकल्प है।

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

में स्विफ्ट 4

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

    text.draw(in: rect, withAttributes: attributeDict) 
संबंधित मुद्दे