中心在UITableViewCell问题中对齐文本

时间:2010-08-12 11:49:37

标签: objective-c iphone ios4

我是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;
}

上述内容并非文本中心。

我也试过了willDisplayCell方法:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.textLabel.textAlignment = UITextAlignmentCenter;
}

我尝试了一些旧的解决方案:

UILabel* label = [[[cell contentView] subviews] objectAtIndex:0];
label.textAlignment = UITextAlignmentCenter;
return cell;

这些都不会对文本对齐产生任何影响。我已经没想到了,任何帮助都会受到最高的赞赏。

提前干杯。

9 个答案:

答案 0 :(得分:125)

不知道它是否有助于解决您的具体问题,但如果您使用UITextAlignmentCenter

initWithStyle:UITableViewCellStyleDefault会有效

答案 1 :(得分:15)

它不起作用,因为textLabel只有任何给定文本所需的宽度。 (UITableViewCell设置为UITableViewCellStyleSubtitle样式时

,在其认为合适时移动标签

您可以覆盖layoutSubviews以确保标签始终填充单元格的整个宽度。

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height);
    self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height);
}

请务必保持高度/ y位置相同,因为只要detailTextLabel的文字为空,textLabel将垂直居中。

答案 2 :(得分:5)

使用此代码:

cell.textLabel.textAlignment = NSTextAlignmentCenter;

以上代码可以使用。 不要使用UITextAlignmentCenter,不推荐使用它。

答案 3 :(得分:4)

使用UITableViewCellStyleSubtitle时,此hack将使文本居中。 用字符串加载两个文本标签,然后在返回单元格之前执行此操作。将每个UILabel添加到每个单元格可能更简单,但我决心找到另一种方式......

// UITableViewCellStyleSubtitle measured font sizes: 18 bold, 14 normal

UIFont *font = [UIFont boldSystemFontOfSize:18]; // measured after the cell is rendered
CGSize size = [cell.textLabel.text sizeWithFont:font];
CGSize spaceSize = [@" " sizeWithFont:font];
float excess_width = ( cell.frame.size.width - 16 ) - size.width;
if ( cell.textLabel.text  &&  spaceSize.width > 0  &&  excess_width > 0 ) { // sanity
    int spaces_needed = (excess_width/2.0)/spaceSize.width;
    NSString *pad = [@"" stringByPaddingToLength:spaces_needed withString:@" " startingAtIndex:0];
    cell.textLabel.text = [pad stringByAppendingString:cell.textLabel.text]; // center the text
}

font = [UIFont systemFontOfSize:14]; // detail, measured
size = [cell.detailTextLabel.text sizeWithFont:font];
spaceSize = [@" " sizeWithFont:font];
excess_width = ( cell.frame.size.width - 16 ) - size.width;
if ( cell.detailTextLabel.text  &&  spaceSize.width > 0  &&  excess_width > 0 ) { // sanity
    int spaces_needed = (excess_width/2.0)/spaceSize.width;
    NSString *pad = [@"" stringByPaddingToLength:spaces_needed withString:@" " startingAtIndex:0];
    cell.detailTextLabel.text = [pad stringByAppendingString:cell.detailTextLabel.text]; // center the text
}

答案 4 :(得分:0)

CustomTableViewCell.m

- (void)layoutSubviews {
  [super layoutSubviews];

    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.contentView.frame.size.width, self.textLabel.frame.size.height);

}

在方法表中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";

  CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
    cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
  }

  cell.textLabel.text = @"Title";
  cell.textLabel.textAlignment = UITextAlignmentCenter;

  return cell;
}

如果需要,self.detailTextLabel

可以重复同样的事情

答案 5 :(得分:0)

在同样的情况下,我使用自定义标签创建了自定义UITableViewCell:

MCCenterTextCell.h文件:

#import <UIKit/UIKit.h>

@interface MCCenterTextCell : UITableViewCell

@property (nonatomic, strong) UILabel *mainLabel;

@end

MCCenterTextCell.m文件:

 #import "MCCenterTextCell.h"


@interface MCCenterTextCell()


@end


@implementation MCCenterTextCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        _mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 30)];
        _mainLabel.font = BOLD_FONT(13);
        _mainLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_mainLabel];

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end

答案 6 :(得分:0)

您可以使用代码将文字置于中心

cell.indentationLevel = 1;

cell.indentationWidth = [UIScreen mainScreen] .bounds.size.width / 2-10;

答案 7 :(得分:0)

如果希望将文本与右侧对齐,我已成功调整了here所述的解决方案。

cell.transform = CGAffineTransformMakeScale(-1.0, 1.0);
cell.textLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
cell.detailTextLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);

答案 8 :(得分:0)

这对我有用...

NSString *text = @"some text";
CGSize size = [text sizeWithAttributes:@{NSFontAttributeName:SOME_UIFONT}];

[cell setIndentationLevel:1];
[cell setIndentationWidth:(tableView.frame.size.width - size.width)/2.0f];

cell.textLabel.font = SOME_UIFONT;
[cell.textLabel setText:text];