coretext自定义字体垂直绘制显示不正确
当我使用coretext时,我发现使用systemfontofsize:
和fontWithName:size:
创建的字体在垂直方向有不同的效果。我不知道这是否正常。谢谢您的帮助。
systemfontofsize
图片:
fontWithName: size:
图片:
此处的标签代码:
@interface TestUILabel: UIView
@end
@implementation TestUILabel
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
// UIFont *font = [UIFont systemFontOfSize:16];
UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
NSAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"hello world"
attributes:@{NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.blackColor,
NSVerticalGlyphFormAttributeName: @(YES)
}];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributeStr);
CFRange rangeAll = CFRangeMake(0,attributeStr.length);
CFRange fitRange = CFRangeMake(0, 0);
CGSize s = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, rangeAll, nil, rect.size, &fitRange);
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1, -1);
CGRect frameRect =CGRectMake((rect.size.width-s.height)/2.0, (rect.size.height-s.width)/2.0, s.height, s.width);
CGPathRef framePath = CGPathCreateWithRect(frameRect, nil);
NSDictionary *dic = @{(id)kCTFrameProgressionAttributeName:@(kCTFrameProgressionLeftToRight)};
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, rangeAll, framePath, (__bridge CFDictionaryRef)dic);
CTFrameDraw(frame, context);
}
@end
When I use coretext, I find that the fonts created with systemfontofsize:
and fontWithName: size:
have different effects in the vertical direction. I don't know if this is normal.Thanks for any help.
systemfontofsize
image:
fontWithName: size:
image:
label code here:
@interface TestUILabel: UIView
@end
@implementation TestUILabel
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
// UIFont *font = [UIFont systemFontOfSize:16];
UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
NSAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"hello world"
attributes:@{NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.blackColor,
NSVerticalGlyphFormAttributeName: @(YES)
}];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributeStr);
CFRange rangeAll = CFRangeMake(0,attributeStr.length);
CFRange fitRange = CFRangeMake(0, 0);
CGSize s = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, rangeAll, nil, rect.size, &fitRange);
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1, -1);
CGRect frameRect =CGRectMake((rect.size.width-s.height)/2.0, (rect.size.height-s.width)/2.0, s.height, s.width);
CGPathRef framePath = CGPathCreateWithRect(frameRect, nil);
NSDictionary *dic = @{(id)kCTFrameProgressionAttributeName:@(kCTFrameProgressionLeftToRight)};
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, rangeAll, framePath, (__bridge CFDictionaryRef)dic);
CTFrameDraw(frame, context);
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Apple 指定将
NSVerticalGlyphFormAttributeName
设置为水平以外的任何内容在 iOS 上都是未定义的。也许这只是其中的一些副作用。https://developer.apple.com/documentation/uikit/nsverticalglyphformattributename
Apple specifies that setting
NSVerticalGlyphFormAttributeName
to anything besides horizontal is undefined on iOS. Maybe this is just some side-effect of that.https://developer.apple.com/documentation/uikit/nsverticalglyphformattributename