Iphone NSString drawInRect:与FONT:和核心文本图形位于同一Draw Rect中
我想画“你好,世界!”两次一次使用NSString的UIStringDrawing类别方法,一次使用核心文本。无论我尝试什么,核心文本版本都是大的。有人能解释为什么吗 下面是我创建的UIView裸体UIView子类的完整实现Iphone NSString drawInRect:与FONT:和核心文本图形位于同一Draw Rect中,iphone,objective-c,ios,core-graphics,core-text,Iphone,Objective C,Ios,Core Graphics,Core Text,我想画“你好,世界!”两次一次使用NSString的UIStringDrawing类别方法,一次使用核心文本。无论我尝试什么,核心文本版本都是大的。有人能解释为什么吗 下面是我创建的UIView裸体UIView子类的完整实现 @implementation CTView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.opaque =
@implementation CTView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.opaque = YES;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef c = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
NSString *helloWorld = @"Hello World!";
UIFont *helloWorldFont = [UIFont systemFontOfSize:16];
CGContextSaveGState(c); {
[helloWorld drawInRect:rect
withFont:helloWorldFont];
} CGContextRestoreGState(c);
CGContextSaveGState(c); {
// setting up attributed string of the catagory name to draw with Core Text
NSMutableAttributedString *name = [[NSMutableAttributedString alloc] initWithString:@"P"];
[name setFont:helloWorldFont];
[name setTextColor:[UIColor blackColor]];
// getting frames for the text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)name);
CFRelease(name);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CGPathRelease(path);
CFRelease(framesetter);
// draw the text
CTFrameDraw(textFrame, c);
CFRelease(textFrame);
} CGContextRestoreGState(c);
}
@end
NSMutableAttributeString通常没有setFont:和setTextColor:方法。您是否添加了实现这些方法的类别?如果是这样,请向我们展示.nsmutableAttributeString通常没有setFont:和setXtcolor:方法。您是否添加了实现这些方法的类别?如果是的话,让我们看看。