IOS CGContextSelectFont 和 CGContextShowText 弃用了现在使用的内容
IOS CGContextSelectFont and CGContextShowText deprecated what to use now
在 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]]));
}
等效替换:AttributedString
[txtString drawAtPoint:CGPointMake(x, y)
withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:12.0]
}];
在 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]]));
}
等效替换:AttributedString
[txtString drawAtPoint:CGPointMake(x, y)
withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:12.0]
}];