我有以下代码......
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中的错误还是我做错了什么?
我是Objective-C和iPhone开发的新手,在尝试将文本放在表格单元格中时遇到了一个问题.我搜索了谷歌,但解决方案是针对旧的SDK错误已修复,这些对我不起作用.
一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Please center me";
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
以上内容并非文本的中心.
我也试过了willDisplayCell方法:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了一些旧的解决方案:
UILabel* label = [[[cell contentView] subviews] objectAtIndex:0];
label.textAlignment = UITextAlignmentCenter;
return cell;
Run Code Online (Sandbox Code Playgroud)
这些都不会对文本对齐产生任何影响.我已经没有想法了,任何帮助都会受到最高的赞赏.
提前干杯.