系统字体“Hiragino Sans”显示剪切上行和下行

我需要在我的应用程序中使用iOS的系统字体“Hiragino Sans W3”,但无论select哪种尺寸/样式或UILabel的尺寸,字体的上行和下行总是出现剪切:

在这里输入图像说明

看来这可以通过创buildUILabel的子类和覆盖方法textRectForBounds:limitedToNumberOfLines:textRectForBounds:limitedToNumberOfLines:返回“正确的”值。 所以下面的代码…

 - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { CGRect result = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; result = CGRectInset(result, 0, -5); return result; } 

…结果在上行和下行不再被剪辑了:

在这里输入图像说明

我知道也可以使用外部编辑器调整字体上升和下降位置。 但是这是一个系统字体 ,不应该没有任何修改正确的工作? 有什么我在这里失踪?

在此先感谢您的任何答案。

我认为这个问题真的归结为字体本身(“Hiragino Sans”)。 使用字体编辑器,可以看到字形超出上行和下行值,这是iOS似乎认为是显示文本的垂直“边界框”。

对于缺乏更好的解决scheme,我一直在使用(相当丑陋的)破解修改UIFont只读属性ascenderlineHeight

文件UIFont+Alignment.h

 #import <UIKit/UIKit.h> @interface UIFont (Alignment) - (void)ensureCorrectFontAlignment; @end 

文件UIFont+Alignment.m

 #import "UIFont+Alignment.h" #import <objc/runtime.h> #import <objc/message.h> static NSHashTable *adjustedFontsList; @implementation UIFont (Alignment) - (void)ensureCorrectFontAlignment { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ adjustedFontsList = [NSHashTable hashTableWithOptions:NSHashTableWeakMemory]; }); @synchronized (adjustedFontsList) { if ([adjustedFontsList containsObject:self]) { return; } else if ([self.fontName containsString:@"Hiragino"]) { SEL originalAscenderSelector = @selector(ascender); Method originalAscenderMethod = class_getInstanceMethod([UIFont class], originalAscenderSelector); SEL originalLineHeightSelector = @selector(lineHeight); Method originalLineHeightMethod = class_getInstanceMethod([UIFont class], originalLineHeightSelector); id result = method_invoke(self, originalAscenderMethod, nil); CGFloat originalValue = [[result valueForKey:@"ascender"] floatValue]; [result setValue:@(originalValue * 1.15) forKey:@"ascender"]; result = method_invoke(self, originalLineHeightMethod, nil); originalValue = [[result valueForKey:@"lineHeight"] floatValue]; [result setValue:@(originalValue * 1.25) forKey:@"lineHeight"]; [adjustedFontsList addObject:self]; } } } @end 

要应用,只需导入标题并在创build新的字体实例后调用[myUIFont ensureCorrectFontAlignment]