IOS CGContextSelectFont和CGContextShowText不赞成现在使用的内容

时间:2015-08-21 05:16:22

标签: ios deprecation-warning

在iOS7中,不推荐使用CGContextSelectFont和CGContextShowText。弃用消息说我必须使用Core Text,但我不知道这段代码的确切等价:

- (void)drawTextInContext:(CGContextRef)context
{
    CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:[NSString defaultCStringEncoding]], self.font.pointSize, kCGEncodingMacRoman);




    CGRect textRect = [self textRectForBounds:self.bounds limitedToNumberOfLines:1];
    CGContextSetTextPosition(context, textRect.origin.x, textRect.origin.y + 5.0f);



    CGContextShowText(context, [self.text cStringUsingEncoding:[NSString defaultCStringEncoding]], strlen([self.text cStringUsingEncoding:[NSString defaultCStringEncoding]]));
}

1 个答案:

答案 0 :(得分:0)

等效替代:AttributedString

[txtString drawAtPoint:CGPointMake(x, y)
           withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"OpenSans"                                                               size:12.0]
                            }];