如何在按钮中使用两行来显示来自两个NSString变量的两行字符串值

时间:2018-04-28 10:33:17

标签: ios objective-c

如何在按钮中使用两行来显示来自两个NSString变量的两行字符串值。我知道如何在按钮中使用两行:

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

但我想从两个NSString变量中获取数据。而不是使用  @"Line1\nLine2" 任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

像这样使用stringWithFormat

NSString* title = [NSString stringWithFormat:@"%@\n%@", onemb, twomb];
[myButton setTitle:title forState: UIControlStateNormal];

答案 1 :(得分:0)

我尝试使用按钮类型为Customsystem

的代码
NSString *onemb = @"hi";
NSString *twomb = @"Welcome";
_myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
_myButton.titleLabel.numberOfLines = 2;
[_myButton setTitle:[NSString stringWithFormat:@"%@\n%@", onemb, twomb] forState: UIControlStateNormal];

我把OP作为

enter image description here

答案 2 :(得分:0)

Swift版本:

let firstLine = "first"
let secondLine = "second"
let button = UIButton()
button.setTitle(firstLine + "\n" + secondLine, for: .normal)