I can't set the title of UIButton using IB as center. My title is multi line.
It is giving like this one
enter image description here

But I want like this one
enter image description here

我已经在这方面留出了空间,但我不想那样做.因为它在某些情况下不是完全对齐的,我知道UILabel有一个属性可以设置对齐,但我不想为此编写代码.我只想从IB开始设置所有内容.

谢谢

推荐答案

解决方案1

可以在情节提要中设置关键路径

将文本设置为您的多行标题,例如hello⌥+↵

您需要按+将文本移动到下一行.

enter image description here

然后添加密钥路径

titleLabel.textAlignment as Number and value 1, 1 means NSTextAlignmentCenter
titleLabel.numberOfLines as Number and value 0, 0 means any number of lines

enter image description here

This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)

如果想查看Xcode上的更改,需要执行以下操作:(记住,可以跳过这些步骤)

  1. 将UIButton子类化以使按钮可设计:

    import UIKit
    @IBDesignable class UIDesignableButton: UIButton {}
    

  2. 将此可设计子类指定给要修改的按钮:

Showing how to change the class of the button using Interface Builder

  1. 如果操作正确,当Designables状态为"最新"(可能需要几秒钟)时,您将在IB中看到可视更新:

Comparing the designable and default button in Interface Builder



解决方案2

If you want to write the code, then do the long process

1.按钮创建IBOutlet
2.在viewDidLoad中写入代码

btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0


解决方案3

在较新版本的Xcode(我的版本是Xcode6.1)中,我们有属性属性title
Select Attributed,然后 Select 文本并按下面的中心选项

P.S.文本不是多行的,因为我必须设置

btn.titleLabel.numberOfLines = 0

在此处输入图像描述

Ios相关问答推荐

如何在SWIFT中处理不可发送的类型?

底部导航栏在iOS上浮动

如何在@AppStorage中存储字体?

拖动手势导致 sim 崩溃 Swift UI

如何在 Flutter 应用程序中测试来自 App/Play Store 的移动深层链接?

如何在 SwiftUI 中将选项卡放在滚动视图中?

PropertyWrapper 不存在的 Codable 属性的默认值

SensorKit - 获取数据不调用结果委托函数

Xcode 6 App Store 提交失败并显示您的帐户已经拥有有效的 iOS 分发证书

如何启用滑动以删除 TableView 中的单元格?

检测 iOS UIDevice 方向

如何在上传到服务器之前在 iOS 上压缩/调整图像大小?

将自定义对象保存到 NSUserDefaults

如何像在 Facebook 应用程序中一样以编程方式打开设置?

用户拒绝使用后如何提示用户打开位置服务

是否可以将 Socket.io 与 AWS Lambda 一起使用?

如何使用完成按钮制作 UIPickerView?

Swift - 获取设备的 WIFI IP 地址

如何在 iOS 中获取正在运行的应用程序的名称

两个数字之间的动画UILabel文本?