【问题标题】:How to handle properly UIButton properties and spaces between multiple lines如何正确处理 UIButton 属性和多行之间的空格
【发布时间】:2015-12-08 19:40:18
【问题描述】:

我使用 Xcode 7、Swift 和 storyboard 部分来创建一个有 2 行的按钮。 这两行居中,但我不能在它们之间添加空格(比如段落间距?)。

按钮现在是这样的:

我使用了故事板方法并结合了代码中的修改。 这是 ViewController.swift 代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var boutonAfficher: UIButton!

    @IBAction func afficheNombres() {
        boutonAfficher.layer.borderColor = UIColor.blueColor().CGColor
       // some other actions after
    }

    @IBAction func TouchDownBoutonAfficher() {
        boutonAfficher.layer.borderColor = UIColor.redColor().CGColor
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        boutonAfficher.layer.borderColor = UIColor.blueColor().CGColor
        boutonAfficher.titleLabel?.textAlignment = NSTextAlignment.Center
        // What to add here to add space between lines ? ParagraphStyle ?
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}


在 Main.storyboard 中,我放置了一个 UIButton 并执行了以下操作:

  • 将按钮(新引用插座)链接到 boutonAfficher
  • 将按钮 (Touch Down) 链接到 TouchDownBoutonAfficher
  • 将按钮 (Touch Up Inside) 链接到 afficheNombres
  • 还通过 Identity Inspector 添加了这个:

所以,我的问题是:

  • 如何在两行之间添加空格?
  • 是否有一种简洁/简单的方法来创建此按钮(圆角、两条线、带边框),而无需混合故事板对象和代码?

提前谢谢你。

【问题讨论】:

  • 对于您的第一个问题,您可以尝试按钮标题中的“\n”,这可能是最简单的方法。你的第二个问题,我也想知道:-)
  • 是的,我也可以添加一个空行,但想知道如何以正确的方式进行(熟练的编码人员如何做到这一点)。所以我们要等待 2 :-)

标签: ios swift uibutton


【解决方案1】:

您可以将 UIButton 的 title 属性从 plain 更改为 attributed,然后更改行距属性。

【讨论】:

  • 谢谢,当您知道如何进行时,它很容易使用。它解决了我眼前的问题:-)
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 2010-09-27
  • 2021-12-14
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多