2013-05-28 21 views
4

我创建了一个自己拥有tableview类的项目。 我有3件事在tableview的任何单元格。 (1个按钮(左边)和2拉布勒状底部图像)如何在单元格tableview上对齐UILabel?

enter image description here

我想标题单元格是右对齐,但我不能这样做。

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

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

U可以尝试有针对的TableView细胞笔尖好让你不必给代码框架和代码将是一个很多清洁剂。 –

+0

尝试使用相同的标签宽度? – tom19830924

+0

@ tom19830924我这样做,但不工作:( – david

回答

1

如果使用的是iOS6的,

使用[_countLabel setTextAlignment:NSTextAlignmentCenter];因为UITextAlignmentCenter在iOS6的弃用。

要右对齐,

[_countLabel setTextAlignment:NSTextAlignmentRight]; 

希望这有助于你...

+0

我的朋友我使用ios 5 – david

+0

使用UITextAlignmentRight而不是NSTextAlignmentRight ... – lakesh

0

您的代码在我结束工作的罚款。请检查您传递给标签的宽度和高度。通过设置标签的宽度和高度来检查你的代码。

希望它可以帮助你。

0
_titleTextField.contentHorizontalAlignment = 
     UIControlContentHorizontalAlignmentRight 
0

首先UITextAlignmentRightdeprecatedin iOS6,所以你应该使用:

_titleTextField.textAlignment = 2; 

其次,你需要检查你的UILabel Frame。请检查您的UILableCGFloat x, CGFloat ywidth

textAlignment = 0(LeftAlignment)。 textAlignment = 1(CenterAlignment)。 textAlignment = 2(RightAlignment)。

希望它有帮助。

0

如果对齐标签内的文本是您的问题,那么上面的解决方案是很好的。 如果设定框架是什么原因呢,我会建议保持代码干净的东西喜欢 -

有一个nibCell.xib文件和相应的.m.hCell.mCell.h文件,该文件是笔尖文件的所有者。 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; 
     } 
0
_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)] 
0

对于iOS 10+ &斯威夫特3+

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