Center Align text in UITableViewCell problem

后端 未结 9 1287
梦如初夏
梦如初夏 2020-12-05 04:02

I\'m kinda new to Objective-C and iPhone development and I\'ve come across a problem when trying to center the text in a table cell. I\'ve searched google but the solutions

9条回答
  •  余生分开走
    2020-12-05 04:40

    It doesn't work because the textLabel is only as wide as it needs to be for any given text. (UITableViewCell moves the labels around as it sees fit when set to the UITableViewCellStyleSubtitle style)

    You can override layoutSubviews to make sure the labels always fill the cell's entire width.

    - (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);
    }
    

    Be sure to keep the height/y-position the same, because as long as the detailTextLabel's text is empty textLabel will be vertically centered.

提交回复
热议问题