2015-07-02 25 views
0

我試圖從在可可(而不是UIKit)中使用Swift的ttf字體生成一個NSImage,而且我正在努力處理當前的上下文創建。從Swift/Cocoa中的TrueType字體生成圖像

我的基本代碼來自這個項目:https://github.com/reeonce/Ionicons.swift 但它是爲UIKit設計的,所以我嘗試將它翻譯爲Cocoa。

這裏是原代碼:

extension UIImage { 
    public class func imageWithIonIcon(icon: Ionicons, height: CGFloat, color: UIColor) -> UIImage { 
     let font = UIFont(name: "ionicons", size: height)! 
     let iconSize = (icon.rawValue as NSString).sizeWithAttributes([NSFontAttributeName: font]) 
     UIGraphicsBeginImageContextWithOptions(iconSize, false, 0.0) 
     (icon.rawValue as NSString).drawAtPoint(CGPointZero, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color]) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image 
    } 
} 

這裏是我的時刻:

extension NSImage { 
    public class func imageWithIonIcon(icon: Ionicons, height: CGFloat, color: NSColor) -> NSImage? { 
     if let font = NSFont(name: "Ionicons", size: height) { 
      let iconSize = (icon.rawValue as NSString).sizeWithAttributes([NSFontAttributeName: font]) 
      let context = CGBitmapContextCreate(nil, Int(iconSize.width), Int(iconSize.height), 8, Int(iconSize.width)*8, CGColorSpaceCreateDeviceRGB(), nil) 
      (icon.rawValue as NSString).drawAtPoint(CGPointZero, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color]) 
      let image = NSImage(CGImage: CGBitmapContextCreateImage(context), size: iconSize) 
      return image 
     } 
     return nil 
    } 
} 

這給了我一個運行時錯誤,因爲我不知道如何初始化上下文:

<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 168 bytes/row. 

任何提示給初學者?由於

回答

0

這裏是繪製在指定的矩形

let context = NSGraphicsContext.currentContext()!.CGContext 

    //// Text Drawing 
    let textRect = NSMakeRect(55, 33, 88, 56) 
    let textTextContent = NSString(string: "Hello, World!") 
    let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle 
    textStyle.alignment = NSTextAlignment.LeftTextAlignment 

    let textFontAttributes = [NSFontAttributeName: NSFont(name: "HelveticaNeue-Bold", size: 17)!, NSForegroundColorAttributeName: NSColor.blackColor(), NSParagraphStyleAttributeName: textStyle] 

    let textTextHeight: CGFloat = textTextContent.boundingRectWithSize(NSMakeSize(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes).size.height 
    let textTextRect: NSRect = NSMakeRect(textRect.minX, textRect.minY + (textRect.height - textTextHeight)/2, textRect.width, textTextHeight) 
    NSGraphicsContext.saveGraphicsState() 
    NSRectClip(textRect) 
    textTextContent.drawInRect(NSOffsetRect(textTextRect, 0, 5), withAttributes: textFontAttributes) 
    NSGraphicsContext.restoreGraphicsState() 

退房上獲取到NSGraphicsContext的文檔要了解他們 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSGraphicsContext_Class/

+0

感謝您的回覆一些文本的例子。不幸的是,我得到了一個致命錯誤:在第一行打開一個可選值時意外地發現了nil(設置上下文常量)。我錯過了什麼嗎? – beeb

+0

糟糕。在10.10中,'graphicsPort'被折舊。嘗試將第一行更改爲'let context = NSGraphicsContext.currentContext()!. CGContext'我將修改我的答案以匹配 – Will

+0

感謝您的更新,不幸的是,該選項無法用此命令解包。錯誤是一樣的... – beeb