CGContextSelectFont等效 [英] CGContextSelectFont equivalent
问题描述
在iOS7中,不推荐使用CGContextSelectFont。弃用消息说我必须使用核心文本,但我不知道这段代码的确切等价:
In iOS7 CGContextSelectFont is deprecated. Deprecation message says that I have to use Core Text, but I don't know which is the exact equivalent of this piece of code:
CGContextSelectFont(context, "Helvetica", kBarLabelSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
CGContextSetTextMatrix (context, CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
CGContextShowTextAtPoint(context, barX, barY, [@"Some text" cStringUsingEncoding:NSUTF8StringEncoding], [barValue length]);
我已经能够使用以下代码创建字体:
I've been able to create the font with this code:
CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), kBarLabelSize, NULL);
CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, font);
但是现在我可以用这种字体画一个文字到上下文中吗?
But now haw can I "draw" a text with this font into the context?
推荐答案
从我的代码中我可以理解,完全相同的是:
As best I can understand from your code, the exact equivalent is:
CGContextSetTextDrawingMode(context, kCGTextFill); // This is the default
[[UIColor blackColor] setFill]; // This is the default
[@"Some text" drawAtPoint:CGPointMake(barX, barY)
withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica"
size:kBarLabelSize]
}];
请注意您调用 CGContextSetTextDrawingMode
和 CGContextSetRGBFillColor
正在将值设置为默认值。使用像这样的UIKit绘图时,不需要调用 CGContextSetTextMatrix
。
Note that your calls to CGContextSetTextDrawingMode
and CGContextSetRGBFillColor
are setting the values to the defaults. Your call to CGContextSetTextMatrix
is not needed when using UIKit drawing like this.
我不知道<$ c但是,$ c> [barValue length] 就在这里。我假设您只是错误地将其用于 @Some text
的长度。 ( length
不是您需要的字节数。您可能意味着 [barValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]
)。
I have no idea what [barValue length]
is here, however. I'm assuming that you simply incorrectly used this for the length of @"Some text"
. (length
is not the number of bytes which is what you need. What you probably meant was [barValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]
).
请注意,UIKit字符串绘图(此处显示)包装了Core Text。
Note that UIKit string drawing (seen here) wraps Core Text.
这篇关于CGContextSelectFont等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!