UIButton中的多行

Csa*_*abi 10 xcode objective-c uibutton

嗨我有问题为我的Button设置多行,这是声明的:

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = UILineBreakModeWordWrap;
button.titleLabel.numberOfLines   = 0;
button.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);

[button addTarget:self 
           action:@selector(myButtonClick) 
 forControlEvents:UIControlEventTouchDown];

button.frame = CGRectMake(0.0, 100.0, 317.0, 100.0);
[button setTitle:string forState:UIControlStateNormal]; 
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.text = @"ahoj";

NSMutableString *ObratString = [[NSMutableString alloc] initWithString:button.titleLabel.text];

[ObratString appendString:@"\n"];
[ObratString appendString:@"caw"];
[ObratString appendString:@"\n"];
[ObratString appendString:@"helllo"];
button.titleLabel.text = ObratString;
[ObratString release];
[self.view addSubview:button];
Run Code Online (Sandbox Code Playgroud)

但最后我才看到第一行.有没有办法使它工作?

Pey*_*loW 57

UIButton显示它的文本与被包含的UILabel.包含标签的默认值是仅显示一行文本.此标签可通过该titleLabel属性访问,您可以对其执行任何可以对正常标签执行的操作.

例如,将其多行打破:

 myButton.titleLabel. numberOfLines = 0; // Dynamic number of lines
 myButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
Run Code Online (Sandbox Code Playgroud)

  • 不推荐使用UILineBreakModeWordWrap.请改用NSLineBreakByWordWrapping (10认同)