coretext自定义字体垂直绘制显示不正确

发布于 2025-01-17 05:51:11 字数 2357 浏览 1 评论 0原文

当我使用coretext时,我发现使用systemfontofsize:fontWithName:size:创建的字体在垂直方向有不同的效果。我不知道这是否正常。谢谢您的帮助。

systemfontofsize 图片:

systemfontofsize

fontWithName: size: 图片:

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:

systemfontofsize

fontWithName: size: image:

fontWithName: size:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-01-24 05:51:11

Apple 指定将 NSVerticalGlyphFormAttributeName 设置为水平以外的任何内容在 iOS 上都是未定义的。也许这只是其中的一些副作用。

在 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.

In iOS, horizontal text is always used and specifying a different value is undefined.

https://developer.apple.com/documentation/uikit/nsverticalglyphformattributename

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文