gpt4 book ai didi

ios7 - iOS 7 中的 CGContextSelectFont 和 CGContextShowTextAtPoint

转载 作者:行者123 更新时间:2023-12-04 14:59:26 24 4
gpt4 key购买 nike

CGContextSelectFont 和 CGContextShowTextAtPoint 在 iOS 7 中已被弃用。 C 中的等价物是什么?

我看到的所有答案都给出了 Objective-C 的等价物(比如使用 NSString 方法),但我正在使用 C++ 文件。

最佳答案

这是解决方案。这些函数已被弃用,取而代之的是 Core Text。它更先进,但需要一段时间才能弄清楚。此示例绘制“Hello World!”使用 Courier 字体。

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

CFStringRef font_name = CFStringCreateWithCString(NULL, "Courier", kCFStringEncodingMacRoman);

CTFontRef font = CTFontCreateWithName(font_name, 36.0, NULL);

CFStringRef keys[] = { kCTFontAttributeName };

CFTypeRef values[] = { font };

CFDictionaryRef font_attributes = CFDictionaryCreate(kCFAllocatorDefault, (const void **)&keys, (const void **)&values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

CFRelease(font_name);

CFRelease(font);

int x = 10;
int y = 10;
const char *text = "Hello World!";

CFStringRef string = CFStringCreateWithCString(NULL, text, kCFStringEncodingMacRoman);

CFAttributedStringRef attr_string = CFAttributedStringCreate(NULL, string, font_attributes);

CTLineRef line = CTLineCreateWithAttributedString(attr_string);

CGContextSetTextPosition(context, x, y);

// Core Text uses a reference coordinate system with the origin on the bottom-left
// flip the coordinate system before drawing or the text will appear upside down
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CTLineDraw(line, context);

CFRelease(line);

CFRelease(string);

CFRelease(attr_string);

CGContextRestoreGState(context);
}

enter image description here

关于ios7 - iOS 7 中的 CGContextSelectFont 和 CGContextShowTextAtPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614815/

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