coretext 自定义字体垂直抽屉显示不正确
coretext custom font vertical draw display not corret
我在使用coretext时,发现用systemfontofsize:
和fontWithName: size:
创建的字体在垂直方向上的效果是不一样的。我不知道这 normal.Thanks 是否有任何帮助。
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
Apple 指定将 NSVerticalGlyphFormAttributeName
设置为水平以外的任何设置在 iOS 上未定义。也许这只是其中的一些 side-effect。
In iOS, horizontal text is always used and specifying a different value is undefined.
https://developer.apple.com/documentation/uikit/nsverticalglyphformattributename
我在使用coretext时,发现用systemfontofsize:
和fontWithName: size:
创建的字体在垂直方向上的效果是不一样的。我不知道这 normal.Thanks 是否有任何帮助。
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
Apple 指定将 NSVerticalGlyphFormAttributeName
设置为水平以外的任何设置在 iOS 上未定义。也许这只是其中的一些 side-effect。
In iOS, horizontal text is always used and specifying a different value is undefined.
https://developer.apple.com/documentation/uikit/nsverticalglyphformattributename