【问题标题】:Center Align text in UITableViewCell problemUITableViewCell中的文本居中对齐问题
【发布时间】:2011-03-28 21:26:17
【问题描述】:

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

这些都不会对文本对齐产生任何影响。我的想法已经用完了,任何帮助将不胜感激。

提前干杯。

【问题讨论】:

  • UITextAlignment 在 iOS 6.0 中已弃用,现在是 NSTextAlignment
  • cell.textLabel?.textAlignment = .center

标签: objective-c iphone ios4


【解决方案1】:

不知道它是否对您的特定问题有帮助,但是如果您使用 initWithStyle:UITableViewCellStyleDefaultUITextAlignmentCenter 确实有效

【讨论】:

  • 这很有帮助,因为我没有对我想要居中的单元格使用细微之处。谢谢。我猜 UITextAlignmentCenter 不适用于 UITableViewCellStyleSubtitle 单元格。
  • UITextAlignmentCenter 现在已弃用。请参阅 this post 了解一个不错的选择。
  • 完美。如果向我们提供未解释如何解决问题的错误消息,则应该解雇 Apple 的某个人。如果不是 stackoverflow 和像你这样的人,我们都会完蛋的。
  • 不是正确答案。 UITextAlignmentCenter 已弃用。改用:cell.textLabel.textAlignment = NSTextAlignmentCenter;
【解决方案2】:

它不起作用,因为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就会垂直居中。

【讨论】:

  • 这些行结合了:self.detailTextLabel.textAlignment = UITextAlignmentRight;self.textLabel.textAlignment = UITextAlignmentRight;正是我正在寻找的,并允许我继续使用 UITableViewCellStyleSubtitle。
【解决方案3】:

使用此代码:

cell.textLabel.textAlignment = NSTextAlignmentCenter;

上面的代码可以工作。 不要使用 UITextAlignmentCenter,它已被弃用。

【讨论】:

    【解决方案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
    }
    

    【讨论】:

      【解决方案5】:

      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 重复相同的操作

      【讨论】:

        【解决方案6】:

        在同样的情况下,我使用自定义标签创建了自定义 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
        

        【讨论】:

          【解决方案7】:

          您可以使用代码使文本居中

          cell.indentationLevel = 1;

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

          【讨论】:

            【解决方案8】:

            如果有人希望将文本向右对齐,我已经成功地调整了here 描述的解决方案。

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

            【讨论】:

              【解决方案9】:

              这对我有用...

              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];
              

              【讨论】:

                猜你喜欢
                • 2012-08-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-07-21
                • 1970-01-01
                • 2018-04-17
                • 1970-01-01
                相关资源
                最近更新 更多