使用 CoreGraphics 绘制字符串/标签时,我们有 2 种方法
- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;
例如:
[value drawInRect:CGRectMake(xValue, rect.origin.y, objLegends.value *singleUnitWidth,heightOfBar)
withFont:[UIFont systemFontOfSize:10.0f]
lineBreakMode:NSLineBreakByClipping
alignment:NSTextAlignmentCenter];
和
CG_EXTERN void CGContextShowText(CGContextRef c, const char *string,
size_t length) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
例如
CGContextSaveGState(context);
// Tanslate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
CGContextSelectFont(context, "Helvetica", 10.0f, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextPosition(context, xValue, rect.size.height-heightOfText);
if ([value canBeConvertedToEncoding:NSMacOSRomanStringEncoding])
{
CGContextShowText(context, [value cStringUsingEncoding:NSMacOSRomanStringEncoding],5);
// strlen([value cStringUsingEncoding:NSMacOSRomanStringEncoding]));
}
CGContextRestoreGState(context);
哪个是好方法,为什么?