Swift - UIButton有两行文字

Sco*_*ott 86 uibutton ios swift

我想知道是否可以用两行文本创建一个UIButton.我需要每一行都有不同的字体大小.第一行是17点,第二行是11点.我已经尝试过将两个标签放在UIButton中,但是我不能让它们留在按钮的范围内.

我试图在ui构建器中完成所有这些操作,而不是以编程方式完成.

谢谢

Sha*_* TK 211

有两个问题.

我想知道是否可以用两行文本创建一个UIButton

这可以通过使用故事板或以编程方式实现.

故事板:

将" 行模式"更改为字符换行或自动换行,并使用Alt/Option + Enter键在UIButton的"标题"字段中输入新行.

在此输入图像描述

编程方式:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
}
Run Code Online (Sandbox Code Playgroud)

我需要每一行都有不同的字体大小1

最糟糕的情况是,您可以使用自定义UIButton类并在其中添加两个标签.

更好的方法是,利用NSMutableAttributedString.请注意,这只能通过编程方式实现.

@IBOutlet weak var btnTwoLine: UIButton?

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    //applying the line break mode
    textResponseButton?.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping;
    let buttonText: NSString = "hello\nthere"

    //getting the range to separate the button title strings
    let newlineRange: NSRange = buttonText.range(of: "\n")

    //getting both substrings
    var substring1 = ""
    var substring2 = ""

    if(newlineRange.location != NSNotFound) {
        substring1 = buttonText.substring(to: newlineRange.location)
        substring2 = buttonText.substring(from: newlineRange.location)
    }

    //assigning diffrent fonts to both substrings
    let font1: UIFont = UIFont(name: "Arial", size: 17.0)!
    let attributes1 = [NSMutableAttributedString.Key.font: font1]
    let attrString1 = NSMutableAttributedString(string: substring1, attributes: attributes1)

    let font2: UIFont = UIFont(name: "Arial", size: 11.0)!
    let attributes2 = [NSMutableAttributedString.Key.font: font2]
    let attrString2 = NSMutableAttributedString(string: substring2, attributes: attributes2)

    //appending both attributed strings
    attrString1.append(attrString2)

    //assigning the resultant attributed strings to the button
    textResponseButton?.setAttributedTitle(attrString1, for: [])
}
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述

  • 您可以将两个行文本都居中对齐。编写以下代码btnTwoLine?.titleLabel?.textAlignment = NSTextAlignment.Center或使用情节提要文件执行此操作(控制部分-> Alignment) (3认同)
  • 效果很好。我现在想知道是否有任何方法可以使文本在每行上居中,以及是否有办法在两行之间插入更多的空间。 (2认同)

Nic*_* S. 20

我正在寻找几乎相同的主题,除了我不需要两种不同的字体大小.如果有人正在寻找一个简单的解决方案:

    let button = UIButton()
    button.titleLabel?.numberOfLines = 0
    button.titleLabel?.lineBreakMode = .byWordWrapping
    button.setTitle("Foo\nBar", for: .normal)
    button.titleLabel?.textAlignment = .center
    button.sizeToFit()
    button.addTarget(self, action: #selector(rightBarButtonTapped), for: .allEvents)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
Run Code Online (Sandbox Code Playgroud)


Mus*_*tri 12

我注意到大多数解决方案中存在的问题是在将换行模式设置为"字符换行"时,第二行将与第一行保持对齐

使所有线条居中.只需将标题从Plain更改为Attributed,然后您可以使每一行居中

归因于中心标题


Sab*_*ana 5

将换行符更改为字符换行,选择按钮,然后在属性检查器中转到换行符并将其更改为字符换行

在此处输入图片说明


Mak*_*zev 5

SWIFT 3语法

let str = NSMutableAttributedString(string: "First line\nSecond Line")
str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 17), range: NSMakeRange(0, 10))
str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(11, 11))
button.setAttributedTitle(str, for: .normal)
Run Code Online (Sandbox Code Playgroud)

  • 不知道为什么,但我必须添加 button.titleLabel?.numberOfLines = 0 (2认同)

A. *_*ejo 5

我已经解决了这个问题,我的解决方案只在故事板中。

变化:

它在Identity Inspector -> User Defined Runtime Attributes(这些 KeyPaths)中添加:

  • 行数 = 2
  • titleLabel.textAlignment = 1

用户定义的运行时属性

我在属性检查器中添加了这个:

  • 换行 = 自动换行

自动换行