我有以下代码......
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds];
buttonLabel.text = @"Long text string";
[targetButton addSubview:buttonLabel];
[targetButton bringSubviewToFront:buttonLabel];
Run Code Online (Sandbox Code Playgroud)
...我的想法是我可以为按钮添加多行文本,但文本总是被UIButton的backgroundImage遮盖.用于显示按钮子视图的日志记录调用显示已添加UILabel,但无法看到文本本身.这是UIButton中的错误还是我做错了什么?
我有以下代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 25, 25);
[[button layer] setCornerRadius:5.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];
[button.titleLabel setFrame:CGRectMake(0,0, 25, 25)];
[button setTitle:[NSString stringWithFormat:@"%@", [[topics objectAtIndex:indexPath.row] unread]] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
问题是,当文本中的字符串不长时,它显示正常(1-2位数).但是,当它很长(3 ++数字)时,我只能看到一个红色按钮,里面没有文字.我该如何调整?
我不这么认为:
[button.titleLabel setAdjustsFontSizeToFitWidth:YES];
Run Code Online (Sandbox Code Playgroud)
这份工作,对吗?