ios - 如何正确处理 UIButton 属性和多行之间的空格

标签 ios swift uibutton

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

按钮现在是这样的:

Button without space between lines

我使用 Storyboard方法结合代码中的修改。 这是 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 并完成了以下操作:

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

Properties

所以,我的问题是:

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

提前谢谢你。

最佳答案

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

Output of your button title

Change the line spacing (5 in my case)

关于ios - 如何正确处理 UIButton 属性和多行之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34154058/

相关文章:

ios - 如何获取某个对象对应于某个 UISegmentedControl 的上下文

ios - 为什么我不能将 UIView 分配给 self.view?

ios - 如何通过 modalView 在应用内购买后重新加载数据

ios - 在 UIViewController 之间传递 UILongPressGestureRecognizer

ios - UIButton 未接收触摸事件

ios - UISegmentedControl 操作中的 UIButton

ios - 屏幕左侧的 UIButton(在 iOS 7 导航滑动区域)未突出显示

ios - 我想为我的iPhone应用程序增加UITableViewStyleGrouped的边框单元格的厚度

ios - Swift 3,无法在 viewcontroller 中创建多个函数?

ios - Swift - 来自字符串的 NumberFormatter 数字对于 iPhone 8 返回 nil 但适用于其他设备