带有系统字体,粗体和字距的子类UIButton

问题描述 投票:0回答:2

您必须使用属性文本进行字距调整。但。另一方面,您无法在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
ios swift uibutton nsattributedstring
2个回答
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)
      }
   }
}

希望对您有帮助


0
投票

复制用于系统字体+字距调整的UIButton:

我整理了贾瓦德的出色信息:

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

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”!)

不可避免地,在任何项目中,您都需要一个带有“ 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中必须做以上所有事情来“调整系统字体”,这真是太神奇了!)

© www.soinside.com 2019 - 2024. All rights reserved.