rai*_*212 19 iphone objective-c core-text ios7
在iOS7中,不推荐使用CGContextSelectFont.弃用消息说我必须使用Core Text,但我不知道这段代码的确切等价物:
CGContextSelectFont(context, "Helvetica", kBarLabelSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
CGContextSetTextMatrix (context, CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
CGContextShowTextAtPoint(context, barX, barY, [@"Some text" cStringUsingEncoding:NSUTF8StringEncoding], [barValue length]);
Run Code Online (Sandbox Code Playgroud)
我已经能够使用以下代码创建字体:
CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), kBarLabelSize, NULL);
CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, font);
Run Code Online (Sandbox Code Playgroud)
但是现在我可以用这种字体"绘制"文本到上下文中吗?
Rob*_*ier 24
我可以从您的代码中了解到最好的,完全相同的是:
CGContextSetTextDrawingMode(context, kCGTextFill); // This is the default
[[UIColor blackColor] setFill]; // This is the default
[@"Some text" drawAtPoint:CGPointMake(barX, barY)
withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica"
size:kBarLabelSize]
}];
Run Code Online (Sandbox Code Playgroud)
请注意,您调用CGContextSetTextDrawingMode和CGContextSetRGBFillColor将值设置为默认值.CGContextSetTextMatrix使用像这样的UIKit绘图时不需要你的电话.
但是,我不知道[barValue length]这里有什么.我假设你只是错误地使用它的长度@"Some text".(length不是你需要的字节数.你可能意味着什么[barValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]).
请注意,UIKit字符串绘制(此处显示)包含Core Text.
voi*_*ref 10
我发现,至少在我的情况下,新的NSString.drawAtPoint接口的问题是它可能颠倒,取决于你如何使用上下文.
另一种方法是使用Core Text方法,特别是CTLine接口:
NSDictionary *attribs = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:14.0]};
NSAttributedString *fontStr = [[NSAttributedString alloc] initWithString:@"some text" attributes:attribs];
CTLineRef displayLine = CTLineCreateWithAttributedString( (__bridge CFAttributedStringRef)fontStr );
CGContextSetTextPosition( ctx, xPosition, yPosition );
CTLineDraw( displayLine, ctx );
CFRelease( displayLine );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11441 次 |
| 最近记录: |