如何在Iphone中对齐中心单元格文本?

时间:2022-04-25 19:31:33

I am unable to align center cell text. I wonder if it may be a problem with XML ?

我无法对齐中心单元格文本。我想知道这是不是XML的问题?

I am create an UILabel, aligning it to the center and adding it to the cell. This is an example of my code:

我正在创建一个UILabel,将其对齐到中心,并将其添加到单元格中。这是我的代码的一个例子:

 if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *lblMenuTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 250, 30)];
NSString *sectionString = [NSString stringWithFormat:@"%@",[[arrDataSection objectAtIndex:indexPath.row] valueForKey:kCasesName]];
[lblMenuTitle setText:sectionString];
[lblMenuTitle setTextAlignment:UITextAlignmentCenter];
lblMenuTitle.font = [UIFont boldSystemFontOfSize:18.0];
    [lblMenuTitle setNumberOfLines:0];
    [lblMenuTitle setTag:[[NSString stringWithFormat:@"1%d",indexPath.row] intValue]];
    [lblMenuTitle setLineBreakMode:UILineBreakModeWordWrap];
[lblMenuTitle setBackgroundColor:[UIColor clearColor]];
[cell addSubview:lblMenuTitle];

CGRect frame;
    CGSize size;

    frame = CGRectMake(20, 5, 250, 30);

    size = [lblMenuTitle.text sizeWithFont:[UIFont systemFontOfSize:18.0]
                               constrainedToSize:CGSizeMake(frame.size.width, 9999)
                                   lineBreakMode:UILineBreakModeWordWrap];

    frame.size = CGSizeMake(size.width+20, size.height);

    lblMenuTitle.frame = frame;
  }

  return cell;

1 个解决方案

#1


4  

You compute the size of your text (size = [lblMenuTitle.text sizeWithFont:…]) and modify the frame of your lblMenuTitle label so that its width is the size of the text (+20px)…

计算文本的大小(size = [lblMenuTitle])。修改lblMenuTitle标签的框架,使其宽度等于文本的大小(+20px)……

So it is quite normal that the text is not centered in your cell : the text is centered in your UILabel ([lblMenuTitle setTextAlignment:UITextAlignmentCenter]) but the label is the same width as the text itself ! So if the text is 100px wide, your label will be 120px wide… but the origin of its frame will still be x=20 !

因此,文本不以您的单元为中心是相当正常的:文本以您的UILabel ([lblMenuTitle settext对齐:UITextAlignmentCenter])为中心,但是标签的宽度与文本本身相同!因此,如果文本宽度为100px,那么标签的宽度为120px,但它的边框的起点仍然是x=20 !

Either keep your label width as large as your cell width, and let the text alignment do its job of centering the text into the label, or if you still want to resize your label the same width as your text, then center its frame too (lblMenuTitle.center = cell.center for example)

要么保持标签宽度与单元格宽度相同,让文本对齐完成将文本居中到标签的工作,要么如果您仍然想将标签的宽度调整为与文本相同的宽度,那么也要将其框架居中(lblMenuTitle)。中心=细胞。例如中心)

#1


4  

You compute the size of your text (size = [lblMenuTitle.text sizeWithFont:…]) and modify the frame of your lblMenuTitle label so that its width is the size of the text (+20px)…

计算文本的大小(size = [lblMenuTitle])。修改lblMenuTitle标签的框架,使其宽度等于文本的大小(+20px)……

So it is quite normal that the text is not centered in your cell : the text is centered in your UILabel ([lblMenuTitle setTextAlignment:UITextAlignmentCenter]) but the label is the same width as the text itself ! So if the text is 100px wide, your label will be 120px wide… but the origin of its frame will still be x=20 !

因此,文本不以您的单元为中心是相当正常的:文本以您的UILabel ([lblMenuTitle settext对齐:UITextAlignmentCenter])为中心,但是标签的宽度与文本本身相同!因此,如果文本宽度为100px,那么标签的宽度为120px,但它的边框的起点仍然是x=20 !

Either keep your label width as large as your cell width, and let the text alignment do its job of centering the text into the label, or if you still want to resize your label the same width as your text, then center its frame too (lblMenuTitle.center = cell.center for example)

要么保持标签宽度与单元格宽度相同,让文本对齐完成将文本居中到标签的工作,要么如果您仍然想将标签的宽度调整为与文本相同的宽度,那么也要将其框架居中(lblMenuTitle)。中心=细胞。例如中心)