iOS - UITableViewCell 文本对齐方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11062707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - UITableViewCell text alignment
提问by Milad
I've added a tableView and dragged a table view cell into it. in the utilities panel, I changed the style to subtitle. I've also tried changing it in code:
我添加了一个 tableView 并将一个表格视图单元格拖入其中。在实用程序面板中,我将样式更改为副标题。我也试过在代码中改变它:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [myArray2 objectAtIndex:indexPath.row];
return cell;
The center alignment doesn't work!
中心对齐不起作用!
I've tried adding a label object to the cell to have a workaround. But I don't know how to access it. even though I assigned an outlet to it, this wouldn't work:
我已尝试向单元格添加标签对象以解决问题。但我不知道如何访问它。即使我为它分配了一个插座,这也不起作用:
cell.labelForCell....
What should I do? any suggestions on how I make it work the usual way, without adding a label to the cell or something?
我该怎么办?关于如何使其以通常的方式工作而不向单元格添加标签或其他内容的任何建议?
回答by Omar Abdelhafith
For the UITableViewCellStyleSubtitle
text alignment cannot be changed
You will have to put a label and add your alignment to it,
To do that you could use this code
对于UITableViewCellStyleSubtitle
无法更改文本对齐方式,您必须放置一个标签并将对齐方式添加到其中,为此您可以使用此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *myLabel;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
myLabel = [[UILabel alloc] initWithFrame:Your_Frame];
//Add a tag to it in order to find it later
myLabel.tag = 111;
//Align it
myLabel.textAlignment= UITextAlignmentCenter;
[cell.contentView addSubview:myLabel];
}
myLabel = (UILabel*)[cell.contentView viewWithTag:111];
//Add text to it
myLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
回答by Ian Hoar
The problem with the subtitle style is that it does a [self.textLabel sizeToFit] when it lays out. When you center in a container that is the perfect size of the contents, nothing changes.
字幕样式的问题在于它在布局时做了一个 [self.textLabel sizeToFit]。当您将容器放在与内容物尺寸完美的容器中时,没有任何变化。
Try this. In a subclass of UITableViewCell, set your textAlignment, and use this as your layoutSubviews code:
尝试这个。在 UITableViewCell 的子类中,设置 textAlignment,并将其用作 layoutSubviews 代码:
- (void)layoutSubviews
{
[super layoutSubviews];
{
CGRect frame = self.textLabel.frame;
frame.size.width = CGRectGetWidth(self.frame);
frame.origin.x = 0.0;
self.textLabel.frame = frame;
}
{
CGRect frame = self.detailTextLabel.frame;
frame.size.width = CGRectGetWidth(self.frame);
frame.origin.x = 0.0;
self.detailTextLabel.frame = frame;
}
}
This makes the textLabel's frame full width, and thus allows the centering effect to be noticeable.
这使得 textLabel 的框架全宽,从而使居中效果变得明显。
Note: since this overrides layoutSubviews, there is a performance cost as it will be called often.
注意:由于这会覆盖 layoutSubviews,因此会产生性能成本,因为它会经常被调用。
回答by Sanjeev Rao
I think adjusting the frame will work. I had style of UITableViewCellStyleValue2 but If i have a bit lengthy text in textLabel, it getting truncated at tail and textAlignment does not work here, so thought of increase the width textLabel.
我认为调整框架会起作用。我有 UITableViewCellStyleValue2 的样式,但是如果我在 textLabel 中有一点冗长的文本,它会在尾部被截断并且 textAlignment 在这里不起作用,所以想增加宽度 textLabel。
-(void)layoutSubviews{
[super layoutSubviews];
if ([self.reuseIdentifier isEqualToString:@"FooterCell"]) {
CGRect aTframe = self.textLabel.frame;
aTframe.size.width += 40;
self.textLabel.frame = aTframe;
CGRect adTframe = self.detailTextLabel.frame;
adTframe.origin.x += 70;
self.detailTextLabel.frame = adTframe;
}
}
回答by malex
It is impossible to change frame for textLabeland detailTextLabelin UITableViewCellright in cellForRowAtIndexPath: method.
这是不可能的变化帧为textLabel和detailTextLabel中的UITableViewCell中的cellForRowAtIndexPath右:方法。
If you don't want to subclass your cell you can implement a small hack using
如果您不想子类化您的单元格,您可以使用
performSelector:withObject:afterDelay:
with zero delay for changing geometry right after default layoutSubviews:
在默认 layoutSubviews之后立即更改几何图形零延迟:
[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0];
See details at here
在此处查看详细信息
回答by Augustin A
Hi please add the following instead of your code,
嗨,请添加以下内容而不是您的代码,
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
change to
改成
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.detailTextLabel.textAlignment = NSTextAlignmentCenter;
it will work fine.
它会正常工作。
回答by Siam
I think the reason is that: the textLabel's width depends on the text length, if the text is too long to show all in a signal line, and you have already set the line break mode and set the number of lines to 0, you will find that the text alignment will be work.
我想原因是:为textLabel的宽度取决于文本的长度,如果文本太长,以显示信号线的所有,你已经设置了换行模式和行数设置为0,你会发现文本对齐将起作用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (nil == cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textColor = [UIColor redColor];
cell.detailTextLabel.textColor = [UIColor greenColor];
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 0;
}
cell.textLabel.text = [NSString stringWithFormat:@"You can initialize a very long string for the textLabel, or you can set the font to be a large number to make sure that the text cann't be shown in a singal line totally:%d", indexPath.row];
return cell;
}
}