mar*_*ter 79 objective-c uibutton uilabel ios
我试图UIButton在其titleLabel中创建一个包含两行文本的文本.这是我正在使用的代码:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,文本只出现在一行上.似乎实现多行文本的唯一方法UIButton.titleLabel是设置numberOfLines=0和使用UILineBreakModeWordWrap.但这并不能保证文本正好是两行.
UILabel但是,使用plain 可以工作:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];
Run Code Online (Sandbox Code Playgroud)
有谁知道如何UIButton使用两条线进行工作?唯一的解决方案是创建一个单独UILabel的文本,并将其添加为按钮的子视图?
Sea*_*ean 145
您不需要将UILabel添加到UIButton.这只是额外的对象和工作.
在按钮的titleLabel上设置这些属性.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0
Run Code Online (Sandbox Code Playgroud)
迅速:
button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0
Run Code Online (Sandbox Code Playgroud)
Bor*_*zin 86
您可以更轻松地完成此操作,无需代码.在Line BreakUIButton上设置的Interface Builder 中Word Wrap.比你可以插入多行标题.只需Option + Return按键即可创建新行.您还需要将其添加到Interface Builder中的用户定义的运行时属性:
titleLabel.textAlignment Number [1]
Run Code Online (Sandbox Code Playgroud)
就这么简单.希望能帮助到你...
Tot*_*mus 76
更新了最新iOS版本的答案
由于这是公认的答案,因此在这里添加了@Sean的答案:
在按钮的titleLabel上设置这些属性.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2; // if you want unlimited number of lines put 0
Run Code Online (Sandbox Code Playgroud)
Swift 3和4:
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0
Run Code Online (Sandbox Code Playgroud)
旧版iOS的原始答案
如果你想要在UIButton你的顶部放置2行文字,你应该UIlabel在它上面添加一个正确的文本.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[myButton addSubview:titleLabel]; //add label to button instead.
Run Code Online (Sandbox Code Playgroud)
更新了界面构建器解决方案
添加了@Borut Tomazin的答案以获得更完整的答案.由于@Borut Tomazin的答案得到了改进,因此再次更新了这一部分.
您可以更轻松地完成此操作,无需代码.在Line BreakUIButton上设置的Interface Builder 中Word Wrap.比你可以插入多行标题.只需Option + Return按键即可创建新行.您还需要将其添加到Interface Builder中的用户定义的运行时属性:
titleLabel.textAlignment Number [1]
Run Code Online (Sandbox Code Playgroud)
Sur*_*mas 20
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
为了完全不需要编辑代码,因此需要对视图进行子类化,在Xcode5及更高版本中,您可以遵循Borut Tomazin的建议:
在Interface Builder (或storyboard)中将Line Break设置为Word Wrap.比你可以插入多行标题.只需按 Option+ Return键即可创建新行.
然后,在用户定义的运行时属性中,您可以添加
关键路径:titleLabel.textAlignment
类型:Number
值:1
注意:这可能不是完全"未来证明",因为我们正在将UITextAlignmentCenter常量转换为其数值(并且该常量可能会随着新iOS版本的发布而改变),但在不久的将来它似乎是安全的.
| 归档时间: |
|
| 查看次数: |
65483 次 |
| 最近记录: |