Center Align text in UITableViewCell problem

后端 未结 9 1257
梦如初夏
梦如初夏 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:15

    This hack will center the text when using UITableViewCellStyleSubtitle. Load both text labels with your strings, then do this before returning the cell. It might be simpler to just add your own UILabels to each cell, but I was determined to find another way...

    // 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
    }
    
    0 讨论(0)
  • 2020-12-05 04:19

    Here is what works for me...

    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];
    
    0 讨论(0)
  • 2020-12-05 04:20

    Use this code:

    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    

    Above code will work. Dont use UITextAlignmentCenter, it is deprecated.

    0 讨论(0)
  • 2020-12-05 04:30

    You could use code to center text

    cell.indentationLevel = 1;

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

    0 讨论(0)
  • 2020-12-05 04:31

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

    In the method table:

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

    If needed, the same thing can be repeated for self.detailTextLabel

    0 讨论(0)
  • 2020-12-05 04:36

    Don't know if it helps your specific problem, however UITextAlignmentCenter does work if you use initWithStyle:UITableViewCellStyleDefault

    0 讨论(0)
提交回复
热议问题