gpt4 book ai didi

macos - Swift/Cocoa 中的 TrueType 字体生成图像

转载 作者:行者123 更新时间:2023-11-30 14:15:55 27 4
gpt4 key购买 nike

我正在尝试使用 Cocoa 中的 Swift(不是 UIKit)从 ttf 字体生成 NSImage,目前我正在努力创建上下文。

我的基本代码来自这个项目: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.

对于初学者有什么提示吗?谢谢

最佳答案

这是在指定矩形中绘制一些文本的示例

    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/

关于macos - Swift/Cocoa 中的 TrueType 字体生成图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31177151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com