【问题标题】:Adjust letter spacing in iOS 7在 iOS 7 中调整字母间距
【发布时间】:2014-01-15 14:54:28
【问题描述】:

在 iOS 7 中,当使用新的从屏幕边缘滑动手势导航返回时,返回按钮的标题(“艺术家”)从粉红色(在下面的示例中)淡出并具有常规字体粗细变成黑色并具有粗体字重。

在我看来,动画使用了两个不同的标签来达到这个效果;一个随着另一个淡入而淡出。然而,Apple 以某种方式调整了字体,使常规标签完美地覆盖了粗体标签,从而产生了单个标签在两种不同的粗细和颜色之间变形的错觉。

他们是否只是简单地调整了常规字体上的字母间距,使其与粗体相匹配?在那种情况下,如何在 iOS 7 中实现呢? Text Kit 是否有任何很棒的功能可以做到这一点,或者我应该如何去做?

【问题讨论】:

    标签: ios7 uilabel textkit


    【解决方案1】:

    你可以像这样调整字母间距,使用NSAttributedString

    在 Objective-C 中:

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"The Clash"];
    [attributedString addAttribute:NSKernAttributeName
                             value:@(1.4)
                             range:NSMakeRange(0, 9)];
        
    self.label.attributedText = attributedString;
    

    在 Swift 3 中:

    let attributedString = NSMutableAttributedString(string: "The Clash")
    attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.4), range: NSRange(location: 0, length: 9))
    
    label.attributedText = attributedString
    

    在 Swift 4 及更高版本中:

    label.attributedText = NSAttributedString(string: "The Clash", attributes: [.kern: 1.4])
    

    有关字距调整的更多信息,请访问Typographical Concepts from the Text Programming Guide

    我不认为有一个 TextKit 功能会自动匹配粗体和常规文本之间的字体间距。

    【讨论】:

    • 有人知道字母间距与字距调整的区别吗?如我所见,NSKernAttributeName 是关于字距调整,而不是关于字母间距。
    • @Andrej 没有跟踪属性 - 请参阅 devsign.co/notes/tracking-and-character-spacing
    • 如何在同一字符串中使用跟踪和字距调整。例如跟踪 = 10,字距调整 = 默认值。 ?
    • 这个答案是正确的,但如果你想从情节提要中做到这一点,请检查:stackoverflow.com/a/35156265/4173671
    • 对于更高版本的密钥已重命名为NSAttributedString.Key.kern
    【解决方案2】:

    对于 Swift 4+,语法很简单:

    let text = NSAttributedString(string: "text", attributes: [.kern: 1.4])
    

    【讨论】:

      【解决方案3】:

      在 Swift 5.3 和 iOS 14 中,NSAttributedString.Key 有一个名为 kern 的静态属性。 kern 有以下declaration

      static let kern: NSAttributedString.Key
      

      此属性的值是一个包含浮点值的NSNumber 对象。此值指定调整紧缩对字符的点数。字距调整可防止在特定字符之间出现不需要的空间,并且取决于字体。值 0 表示禁用字距调整。此属性的默认值为 0。

      以下 Playground 代码显示了kern 的可能实现,以便在您的NSAttributedString 中有一些字母间距:

      import PlaygroundSupport
      import UIKit
      
      class ViewController: UIViewController {
          override func viewDidLoad() {
              super.viewDidLoad()
      
              let string = "Some text"
              let paragraph = NSMutableParagraphStyle()
              paragraph.alignment = .center
              let attributes: [NSAttributedString.Key: Any] = [
                  NSAttributedString.Key.kern: 10,
                  NSAttributedString.Key.paragraphStyle: paragraph
              ]
              let attributedString = NSMutableAttributedString(
                  string: string,
                  attributes: attributes
              )
      
              let label = UILabel()
              label.attributedText = attributedString
      
              view.backgroundColor = .white
              view.addSubview(label)
      
              label.translatesAutoresizingMaskIntoConstraints = false
              NSLayoutConstraint.activate([
                  label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                  label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
              ])
          }
      }
      
      PlaygroundPage.current.liveView = ViewController()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 2015-07-29
        • 1970-01-01
        • 2012-10-25
        相关资源
        最近更新 更多