在iOS 7中不推荐使用CGContextSelectFont和CGContextShowTextAtPoint.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,self.bounds.size.height); CGContextScaleCTM(context,1.0,-1.0); CTLineDraw(line,context); CFRelease(line); CFRelease(string); CFRelease(attr_string); CGContextRestoreGState(context); }