【问题标题】:Letter spacing in iOSiOS中的字母间距
【发布时间】:2013-10-19 20:10:42
【问题描述】:

我有以下代码:

[[cancelButton titleLabel] setFont:[UIFont fontWithName:@"ProximaNova-Regular" size:15]];

我该如何设置字母间距?

【问题讨论】:

  • 这是一个 iPhone 应用程序。

标签: ios objective-c uilabel uifont


【解决方案1】:

abstract 中的字母间距是不能改变的,也就是说在 iOS 5 及以下版本下根本不能改变。

从 iOS 6 开始,您可以将属性字符串而不是普通字符串推送到 UILabel。推送属性字符串的过程与推送普通字符串的过程略有不同——字体、文本颜色和一堆其他属性都设置在字符串而不是标签上。原因是属性字符串允许为字符串的不同区域设置不同的属性。所以你可以设置一个字符串,组合多种字体、文本颜色等。

支持的 Core Text 属性之一是 kCTKernAttributeName,从 iOS 6 开始,通过 UIKit 添加 NSKernAttributeName 更容易利用它。您可以使用字距调整字形的水平间距。

在 iOS 5 和更早的版本中,您曾经不得不在 Core Foundation C 样式对象和 Objective-C UIKit 对象之间来回切换。从 6 开始,不再需要。但要小心,如果你搜索 6 岁以下的事情变得容易得多的网络——如果你看到很多 __bridge 强制转换和手动 CFReleases,那么你可能正在查看旧代码。

无论如何,假设您目前有类似的东西:

UILabel *label = [cancelButton titleLabel];

UIFont *font = <whatever>;
UIColor *textColour = <whatever>;
NSString *string = <whatever>;

label.text = string;
label.font = font;
label.textColor = textColour;

你会做更多类似的事情:

NSAttributedString *attributedString = 
    [[NSAttributedString alloc]
          initWithString:string
          attributes:
              @{
                    NSFontAttributeName : font,
                    NSForegroundColorAttributeName : textColour
              }];

label.attributedText = attributedString;

在您的情况下,还要调整您要添加的整体字距调整:

NSAttributedString *attributedString = 
    [[NSAttributedString alloc]
          initWithString:string
          attributes:
              @{
                    NSFontAttributeName : font,
                    NSForegroundColorAttributeName : textColour,
                    NSKernAttributeName : @(-1.3f)
              }];

label.attributedText = attributedString;

或您想要应用的任何字距调整值。请参阅 NSAttributedString UIKit Additions Reference 底部的各种常量,了解您可以应用的各种其他属性以及它们首先在哪个 iOS 版本上可用。

很久以后的附录:虽然仍然是您会遇到的最不 Swifty 的人之一,但我认为这是 Swift 中的等价物:

button.titleLabel?.attributedText =
    NSAttributedString(
        string: string,
        attributes:
        [
            NSFontAttributeName: font,
            NSForegroundColorAttributeName: textColour,
            NSKernAttributeName: -1.3
        ])

【讨论】:

  • 你是如何使用 Swift 的?
  • @lnguyen55 我可能是你遇到的最差的 Swift 程序员;尽管如此,我还是快速完成了。
【解决方案2】:

支持的核心文本属性之一是 kCTKernAttributeName,从 iOS 6 开始,通过 UIKit 添加 NSKernAttributeName 更容易利用它。您可以使用字距调整字形的水平间距。

字距调整是调整两个唯一字母之间的间距。字距在不同的字符对之间变化。例如,像“AVA”这样的组合在字符之间的字距调整与像“VVV”这样的组合不同

通过使用 NSKernAttributeName,您实际上覆盖了字体文件中内置的自定义空间调整,将所有不同的字符对紧缩值设置为相同的数字,从而破坏了最佳紧缩。当应用于整个文本字符串时,少量的字距调整会更明显。但是,高字距值可能会将字母拉得足够远,以至于间距不佳不会那么明显。

您正在寻找的是跟踪(又名字母间距),它是给定文本块中所有字母之间的间距。不幸的是,iOS 似乎不允许您控制该属性。

【讨论】:

  • 我们可以改变前导和字母间距吗?
【解决方案3】:
    NSAttributedString *cancelButtonAttributedString = [[NSAttributedString alloc]
                                           initWithString:@"Hello"
                                           attributes:
                                           @{
                                             NSKernAttributeName: @(1.5)
                                             }];
    [cancelButton setAttributedTitle:cancelButtonAttributedString forState:UIControlStateNormal];

这只是上述问题的简单答案

【讨论】:

  • 绝妙的答案,切中要害。这只改变了间距,(保持所有以前设置的字体属性不变),这就是问题所在。
  • 领先,是的。 NSParagraphStyle 具有您可以设置的 lineSpacing。 (Docs) 请注意,它设置了 between 行之间的空间,这是“前导”的原始定义:字面意思是放置在行之间以将它们隔开的前导的高度。我被教导将领先视为字体大小 + 行距。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
相关资源
最近更新 更多