ios - 2 行文本 UIButton

标签 ios objective-c iphone swift uibutton

我有一个 UIButton,按钮的文本由随机发生器填充。 但是我的问题是有时文本中的字符数太多,导致按钮现在显示整个文本。

是否可以检查字符是否太多,然后让它将其余文本放到另一个文本行?所以基本上必须为 UIButton 添加文本行而不是一行?

最佳答案

Objectivc-C

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if u need

否则使用这个

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];

swift

button.titleLabel!.lineBreakMode = .ByWordWrapping
button.titleLabel!.numberOfLines = 2
button.titleLabel!.textAlignment = .Center

否则使用这个

button.titleLabel!.lineBreakMode = .ByWordWrapping
button.titleLabel!.textAlignment = .Center
button.setTitle("Line1\nLine2", forState: .Normal)

Swift3

buttonName.titleLabel!.lineBreakMode = .byWordWrapping
buttonName.titleLabel!.textAlignment = .center
buttonName.setTitle("Line1\nLine2", for: .normal)

关于ios - 2 行文本 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29280772/

相关文章:

ios - AFNetworking - 序列化连接

ios - 将 Collection View 添加到 TableView 之一

objective-c - 从 CGPoint 获取 SCNVector3

ios - 选中 UITableView 时自动将行移动到底部

ios - 从跨多个 View Controller 的 TableView 单元显示警报 Controller

ios - 以编程方式覆盖 loadView 并设置 rootViewController,无 Storyboard

ios - 使用 Telprompt ://to make a call (editing message)

ios - CocoaPods 更新正在降级已安装的 pod

iphone - 如何使用 UIPageControl 从一个 View 导航到另一 View ?

iphone - 检查 iPhone 应用程序中使用的 SDK 方法