2010-08-12 69 views
58

我对Objective-C和iPhone的开发颇为陌生,在尝试将表格中的文本居中时遇到了问题。我搜索谷歌,但解决方案是一个旧的SDK错误,已修复,这些不适合我。居中对齐UITableViewCell问题中的文本

一些代码:

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

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

提前欢呼。

+11

UITextAlignment中的iOS 6.0过时了,它现在是NSTextAlignment – Souljacker 2012-10-19 09:05:27

回答

120

不知道它是否有助于您的具体问题,如果你使用initWithStyle:UITableViewCellStyleDefault

+4

这帮助,因为我没有使用微妙的,因为我想中心的细胞。谢谢。我猜UITextAlignmentCenter不适用于UITableViewCellStyleSubtitle单元格。 – Magpie 2010-08-31 16:28:56

+2

现在不建议使用UITextAlignmentCenter。看看[这篇文章](http://stackoverflow.com/a/12793054/2521004)是一个很好的选择。 – 2014-07-11 15:14:05

+2

完美。苹果公司的某个人应该被解雇,向我们提供错误信息,但这些信息并不能解释如何解决问题。如果它不是stackoverflow和像你这样的人,我们会全部搞砸。 – SpaceDog 2014-10-19 19:36:15

4

本hack使用UITableViewCellStyleSubtitle时将文本居中但是UITextAlignmentCenter确实工作。 将两个文本标签加载到您的字符串中,然后在返回单元格之前执行此操作。这可能是简单到只需添加你自己的UILabels到每个细胞,但我下定决心要找到另一种方式......

// 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 
} 
+1

对我不起作用 – marciokoko 2013-07-18 21:55:34

15

它不工作,因为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将垂直居中。

+0

这些行结合在一起:'self.detailTextLabel.textAlignment = UITextAlignmentRight;'和'self.textLabel.textAlignment = UITextAlignmentRight;'_exactly_我​​正在寻找并且允许我继续使用UITableViewCellStyleSubtitle。 – 2012-07-01 21:44:23

0

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

0

在我创建的自定义的UITableViewCell有同样的情况自定义标签:

MCCenterText Cell.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 
3

使用此代码:

cell.textLabel.textAlignment = NSTextAlignmentCenter; 

上面的代码将正常工作。 不使用UITextAlignmentCenter,它已被弃用。

+0

确实这可以工作 - 谢谢! – 2016-05-26 08:01:34

0

你可以使用代码来居中文本

cell.indentationLevel = 1;

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

0

如果希望将文本对齐到右侧,我已经成功地使用了描述here的解决方案。

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