如何使 UIButton 的文本居中对齐?使用 IB [英] How to make UIButton's text alignment center? Using IB

查看:15
本文介绍了如何使 UIButton 的文本居中对齐?使用 IB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法以 IB 为中心设置 UIButton 的标题.我的标题是多行的.
它是这样的

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

但我想要这个

But I want like this one

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

I have given space in this but I don't want to do that. As it is not aligned exactly for some cases and I know there is a property of UILabel to set the alignment but I don't want to write a code for that.. just want to set everything from IB.

谢谢

推荐答案

Solution1

您可以在故事板中设置关键路径

Solution1

You can set the key path in the storyboard

将文本设置为多行标题,例如你好 + 多行

Set the text to your multiline title e.g. hello + multiline

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

You need to press + to move text to next line.

然后添加关键路径

titleLabel.textAlignmentNumber 和值 11 表示 NSTextAlignmentCenter
titleLabel.numberOfLinesNumber 和值 00 表示任意行数

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

这不会反映在 IB/Xcode 上,但会在运行时处于中心位置(设备/模拟器)

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

If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)

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

  1. Subclass the UIButton to make the button designable:

import UIKit
@IBDesignable class UIDesignableButton: UIButton {}

将此可设计的子类分配给您正在修改的按钮:

Assign this designable subclass to the buttons you're modifying:

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


如果你想写代码,那就做漫长的过程

1.为按钮
创建IBOutlet2.在viewDidLoad中编写代码

1.Create IBOutlet for button
2.Write code in viewDidLoad

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

<小时>




在较新版本的 xcode(我的是 xcode 6.1)中,我们有属性属性标题
选择 Attributed 然后选择文本并按下下面的中心选项

In newer version of xcode (mine is xcode 6.1) we have property attributed title
Select Attributed then select the text and press centre option below

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

P.S. The text was not coming multiline for that I have to set the

btn.titleLabel.numberOfLines = 0

这篇关于如何使 UIButton 的文本居中对齐?使用 IB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆