【问题标题】:Subclass UIButton with system font, bold, and kerning具有系统字体、粗体和字距调整的 UIButton 子类
【发布时间】:2020-09-16 07:32:02
【问题描述】:

您必须使用属性文本来进行字距调整。但。另一方面,您无法在 IB 属性文本菜单中获取系统字体。

我如何(在代码中)创建一个UIButton,它有

  • 系统字体,粗体粗体
  • 尺寸 11
  • kern 属性设置为 -2

理想情况下,它会从 IB 中的纯文本标题中收集按钮的文本。但是,如果必须在代码中设置文本,那很好。

class NiftyButton: UIButton { 
    ????
}

通常我像这样初始化 UIButton .. 但我什至不知道这是否是最好的地方? (你不能在 layoutSubviews 中这样做,因为它当然会循环。)

class InitializeyButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        common()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        common()
    }

    func common() {
        ...
    }
}

如何在代码中实现...

  • 系统字体,粗体粗体
  • 尺寸 11
  • kern 属性设置为 -2

【问题讨论】:

  • 您可以在 Interface Builder 中不使用“代码”来完成。我让它适用于字距调整、大小字体和粗体,但无法指定系统之一。只有一个有名字的。
  • 没错,所以问题来了,Larme!

标签: ios swift uibutton nsattributedstring


【解决方案1】:

这是您可以用来获得所需结果的类:

class InitializeyButton: UIButton {

   @IBInspectable var spacing:CGFloat = 0 {
      didSet {
        updateTitleOfLabel()
      }
   }

   override func setTitle(_ title: String?, for state: UIControl.State) {

        let color = super.titleColor(for: state) ?? UIColor.black
        let attributedTitle = NSAttributedString(
            string: title ?? "",
            attributes: [NSAttributedString.Key.kern: spacing,
                         NSAttributedString.Key.foregroundColor: color,
                         NSAttributedString.Key.font: UIFont.systemFont(ofSize: self.titleLabel?.font.pointSize ?? 11, weight: .bold)   ])
        super.setAttributedTitle(attributedTitle, for: state)
   }

  private func updateTitleOfLabel() {
    let states:[UIControl.State] = [.normal, .highlighted, .selected, .disabled]
      for state in states {
        let currentText = super.title(for: state)
        self.setTitle(currentText, for: state)
      }
   }
}

【讨论】:

    【解决方案2】:

    复制粘贴 UIButton 用于 systemFont + kerning:

    我整理了贾瓦德的绝妙资料:

    首先,在启动时,您必须创建属性标题:

    import UIKit
    
    class SmallChatButton: UIIButton { // (note the extra "I" !!)
        
        override func common() {
            super.common()
            backgroundColor = .your corporate color
            contentEdgeInsets = UIEdgeInsets(top: 7, left: 11, bottom: 7, right: 11)
            titles()
        }
        
        private func titles() {
            let states: [UIControl.State] = [.normal, .highlighted, .selected, .disabled]
            for state in states {
                let currentText = super.title(for: state)
                setTitle(currentText, for: state)
            }
        }
        
    

    所以要做到这一点..

        override func setTitle(_ title: String?, for state: UIControl.State) {
    
            let _f = UIFont.systemFont(ofSize: 10, weight: .heavy)
            
            let attributedTitle = NSAttributedString(
                string: title ?? "Click",
                attributes: [
                    NSAttributedString.Key.kern: -0.5,
                    NSAttributedString.Key.foregroundColor: UIColor.white,
                    NSAttributedString.Key.font: _f
            ])
            setAttributedTitle(attributedTitle, for: state)
        }
        
        override func layoutSubviews() { // example of rounded corners
            super.layoutSubviews()
            layer.cornerRadius = bounds.height / 2.0
            clipsToBounds = true
        }
    }
    

    请注意,“-0.5”字距是大多数排版师想要的典型“稍微紧的字体”

    全部大写、略粗的字体或小字体看起来都不错。 Apple 测量系统不同于排版师使用的任何系统,因此,您只需对其进行更改,直到您应用上的排版师满意为止。

    什么是UIIButton(注意多余的“我”!!)

    在任何项目中,您都不可避免地需要一个具有“I”初始化器的 UIButton,因此您可能会拥有:

    import UIKit
    
    class UIIButton: UIButton {
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            common()
        }
        
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            common()
        }
        
        func common() {
            
        }
    
    }
    

    (令人惊奇的是,在 iOS 中必须执行上述所有操作才能“紧缩系统字体”!)

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 2013-12-03
      • 2011-12-14
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      相关资源
      最近更新 更多