【问题标题】:How to right Align UILabel on cell tableview?如何在单元格表格视图上正确对齐 UILabel?
【发布时间】:2013-05-23 00:48:11
【问题描述】:

我要创建一个在 self.tableview 类中的项目。 我在 tableview 的任何单元格中有 3 件事。 (1 个按钮(左侧)和 2 个类似底部图像的标签)

我希望标题单元格正确对齐,但我做不到。

这是我的代码。我的错误在哪里:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"copymove-cell-bg"]];
        [_backgroundImageView setContentMode:UIViewContentModeTopRight];
        [self setBackgroundView:_backgroundImageView];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];

        _iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_iconButton setFrame:CGRectMake(0, 10, 47, 50)];
        [_iconButton setAdjustsImageWhenHighlighted:NO];
        [_iconButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder"] forState:UIControlStateNormal];
        [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder-selected"] forState:UIControlStateSelected];
        [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder-selected"] forState:UIControlStateHighlighted];
        [self.contentView addSubview:_iconButton];

        _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, _titleTextField.frame.size.width, _titleTextField.frame.size.height)];
        [_titleTextField setFont:KOFONT_FILES_TITLE];
        [_titleTextField setTextColor:KOCOLOR_FILES_TITLE];
        [_titleTextField.layer setShadowColor:KOCOLOR_FILES_TITLE_SHADOW.CGColor];
        [_titleTextField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin];
        [_titleTextField.layer setShadowOffset:CGSizeMake(0, 1)];
        [_titleTextField.layer setShadowOpacity:1.0f];
        [_titleTextField.layer setShadowRadius:0.0f];
        [_titleTextField setTextAlignment:UITextAlignmentRight];
        [_titleTextField setLineBreakMode:UILineBreakModeMiddleTruncation];
        [self.contentView addSubview:_titleTextField];
        [self.layer setMasksToBounds:YES];

        _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(273, 29, 47, 28)];
        [_countLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin];
        [_countLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"item-counter"]]];
        [_countLabel setTextAlignment:UITextAlignmentCenter];
        [_countLabel setLineBreakMode:UILineBreakModeMiddleTruncation];
        [_countLabel setFont:KOFONT_FILES_COUNTER];
        [_countLabel setTextColor:KOCOLOR_FILES_COUNTER];
        [_countLabel setShadowColor:KOCOLOR_FILES_COUNTER_SHADOW];
        [_countLabel setShadowOffset:CGSizeMake(0, 1)];

        [self setAccessoryView:_countLabel];
        [self.accessoryView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin];
    }
    return self;
}

【问题讨论】:

  • 你可以尝试为 TableView Cell 设置一个笔尖,这样你就不必在代码中给出框架,代码会更简洁。
  • 尝试使用相同的标签宽度?
  • @tom19830924 我做到了,但没有工作:(
  • @Ishank 我的朋友请告诉我更多信息!!!
  • 我的朋友如何用xib文件创建自定义单元格并放入UITable View?

标签: ios objective-c uitableview alignment uilabel


【解决方案1】:

如果您使用的是 iOS6,

使用[_countLabel setTextAlignment:NSTextAlignmentCenter];,因为 UITextAlignmentCenter 在 iOS6 中已被弃用。

要右对齐,

[_countLabel setTextAlignment:NSTextAlignmentRight];

希望对你有帮助...

【讨论】:

    【解决方案2】:

    您的代码在我这边运行良好。请检查您传递给标签的宽度和高度。只需通过设置标签的宽度和高度来检查您的代码。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:
      _titleTextField.contentHorizontalAlignment =  
               UIControlContentHorizontalAlignmentRight
      

      【讨论】:

        【解决方案4】:

        如果对齐标签内的文本是您的问题,那么上述解决方案很好。 如果设置框架是问题,我建议保持代码干净,例如 -

        有一个nibCell.xib 文件和一个对应的.m.hCell.mCell.h 文件,它是nib 文件的所有者。 Cell 类应该是 UITableViewCell 的子类。将UILables/UIImageView/UIButton 放在xib 中所需的位置,并将它们映射到Cell 类中的相应属性(在Cell.xib 中也给出一些Cell identifier)。

        cellForRowatIndexdatasource 方法中从笔尖中获取单元格-

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
            Cell *cell = (Cell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//same CellIdentifier as in the Cell.xib
                            if(cell==nil){
                         cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell"  owner:self        options:nil] lastObject]; 
                    }
        
                //Access and set the labels, imageViews here
        
                    return cell;
                }
        

        【讨论】:

          【解决方案5】:
          _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, _titleTextField.frame.size.width, _titleTextField.frame.size.height)];
          

          这很奇怪。您正在根据标签自身的宽度和高度设置标签的宽度和高度。这会将其设置为前一个标签的宽度和高度。我不知道你第一次遇到这段代码会做什么。

          您的文本在标签视图的边界内向右对齐,但边界并没有在您想要的位置结束。您可以尝试设置背景颜色,以便查看标签框架的位置。

          _titleTextField.backgroundColor = [UIColor yellowColor];
          

          您应该将其设置为填充单元格的空间,并让它在其中定位文本。使标签的框架过大不会影响文本的大小,但过小可能会影响文本的大小,具体取决于设置的选项。

          _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, 273 - 69, self.contentview.bounds.size.height)]
          

          【讨论】:

            【解决方案6】:

            第一个UITextAlignmentRightdeprecated in iOS6,所以你应该使用:

            _titleTextField.textAlignment = 2;
            

            其次,您需要检查您的UILabel Frame。检查CGFloat x, CGFloat ywidthUILable

            textAlignment = 0(左对齐)。 textAlignment = 1(居中对齐)。 textAlignment = 2 (右对齐)。

            希望对你有帮助。

            【讨论】:

              【解决方案7】:

              适用于 iOS 10+ 和 Swift 3+

              titleTextField.textAlignment = .center
              lblTitle.textAlignment = .right
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-01-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多