【问题标题】:NSString sizing does not account for Greek breathing marksNSString 大小不考虑希腊呼吸标记
【发布时间】:2015-03-10 17:19:24
【问题描述】:

我有一个显示希腊文字的应用程序。我使用Cardo 字体以获得良好的显示效果。在开发 AppleWatch 扩展程序和应用程序时,有人向我指出,一些特殊字符被截断了。这是一些示例文本的外观(来自 iPhone 模拟器的屏幕截图):

这是 Watch 模拟器上的相同文本:

请注意,第一个单词的第二个字符上的花哨的重音字符(具体来说,是带有抑扬音符号的呼吸标记)被切断。我尝试使用一些NSString 测量代码在手机上设置标签的框架,如下所示:

UILabel *label = [[UILabel alloc]init];
label.font = [UIFont fontWithName:@"Cardo" size:16];
[self.view addSubview:label];
label.text = @"οὗτος ἦλθεν εἰς μαρτυρίαν ἵνα μαρτυρήσῃ περὶ τοῦ φωτός, ἵνα πάντες πιστεύσωσιν δι᾽ αὐτοῦ.";
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.lineBreakMode = NSLineBreakByWordWrapping;

CGRect rect = [label.text boundingRectWithSize:self.view.bounds.size
  options:NSStringDrawingUsesLineFragmentOrigin
  attributes:@{NSFontAttributeName: label.font,
     NSParagraphStyleAttributeName: style} context:nil];
label.frame = CGRectMake(5, 100, ceilf(rect.size.width), ceilf(rect.size.height));
label.layer.borderWidth = 1; //for clarity
label.layer.borderColor = [UIColor blackColor].CGColor;

结果如下所示(为清楚起见画了边框):

有趣的是,如果我使用系统字体而不是 Cardo,额外的符号会正确显示:

所以,我的问题:是什么原因导致NSString 尺寸会切断多余的标记?是否有一些选项我可以传递给大小调整方法来纠正这个问题?或者更好的是,我可以在 Watch 应用的 WKInterfaceLabel 上设置一些选项以使其正确呈现吗?

【问题讨论】:

    标签: ios unicode nsstring wkinterfacelabel


    【解决方案1】:

    我不知道这是否会解决它,但这会给你更多的研究。我遇到了使用“小写”数字的字体问题,这是一种表格数字的排版样式。我能够使用强制所有数字大写的 UIFontDescription 创建字体的变体。在您的情况下,字体的上升器超出字体的 capHeight 的顶部。使用字体描述符创建字体有很多选项,其中一个可能会有所帮助。这是我创建大写数字字体的方法。

    _font = [UIFont fontWithName:_fontName size:size];
    
    NSArray* featureSettings = @[
                @{
                     UIFontFeatureTypeIdentifierKey: @(kNumberCaseType),
                     UIFontFeatureSelectorIdentifierKey: @(kUpperCaseNumbersSelector)
                }];
    
    
    UIFontDescriptor* originalDescriptor = [_font fontDescriptor];
    UIFontDescriptor* newDescriptor = [originalDescriptor fontDescriptorByAddingAttributes: @{UIFontDescriptorFeatureSettingsAttribute: featureSettings }];
    _font = [UIFont fontWithDescriptor: newDescriptor size: size];
    

    具体检查 kVerticalPositionType。

    我实际上在我的 iOS 应用程序中遇到了一个案例,其中字体上升器从基线向上超过 capHeight 并且我的属性字符串被切断。因为我在使用石英绘图代码,所以我只是使用字体的 lineHeight 与其上升器的差异并填充顶部。不幸的是,这不是 Apple Watch 上的选项。

    【讨论】:

      【解决方案2】:

      我怀疑问题出在字体本身,上升器设置得太紧而无法正常显示。

      我会首先尝试通过创建 UILabel 的子类并覆盖 drawTextInRect 来设置 UILabel 的文本插入:

      - (void)drawTextInRect:(CGRect)rect {
      UIEdgeInsets insets = {5, 0, 0, 0};
      [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
      }
      

      如果您尝试增加文本插入以适应字体但没有奏效,请查看Custom installed font not displayed correctly in UILabel

      【讨论】:

      • 我将上升器从 1800 增加到 2000,它似乎工作得很好。
      猜你喜欢
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多