【问题标题】:UILabel text alignment rightUILabel 文本右对齐
【发布时间】:2012-06-29 00:40:57
【问题描述】:

我希望我的文本应该右对齐。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"lisn"];
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"] autorelease];
CGSize  textSize = { 210.0, 10000.0 };
CGSize size = [[gMessageArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

UILabel *lisnerMessage=[[[UILabel alloc] init] autorelease];
lisnerMessage.backgroundColor = [UIColor clearColor];
[lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];
lisnerMessage.numberOfLines=0;
lisnerMessage.textAlignment=UITextAlignmentRight;
lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:lisnerMessage];
return cell
}

但我的文字没有对齐请帮助

【问题讨论】:

  • 这不是对您问题的回答,而是对逻辑的评论。您确定要在每次刷新表格时为您的单元格添加一个额外的标签吗?
  • @PhillipMills:对于每个单元格,我有不同的文本,而不是该单元格中的额外文本
  • 除非有代码可以从单元格中删除旧标签,否则每次调用特定索引路径时,您发布的内容总是会再添加一个标签。
  • @PhillipMills:是的,我在尝试什么?

标签: objective-c ios xcode ios5


【解决方案1】:

因为您使用的是 sizeWithFont,然后将框架设置为该大小,所以您的文本会右对齐。尝试在标签中添加浅灰色的背景色,看看我在说什么。您的标签应设置为与表格单元格相同的大小,并允许文本在其中流动。然后它会向右对齐。

样本更新

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lisn"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"lisn"];

    UILabel *lisnerMessage = [[UILabel alloc] init];
    lisnerMessage.backgroundColor = [UIColor clearColor];
    [lisnerMessage setFrame:cell.frame];
    lisnerMessage.numberOfLines = 0;
    lisnerMessage.textAlignment = NSTextAlignmentRight;
    lisnerMessage.text = [gMessageArray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lisnerMessage];

    return cell
}

【讨论】:

  • :您能否详细说明或添加一些关于您的答案的代码。这对我很有帮助
  • 是的,使用不同背景颜色的技巧是一个很好的技巧。这将有助于 Alok 了解会发生什么。原则上使用 sizeWithFont 是可以的,但我不会使用 size.with 作为框架。使用 [lisnerMessage setFrame:CGRectMake(75 , 20, textSize.width + 5, size.height+2)];而是。
  • 我已经编辑了您的代码,其中包含所需更改的粗略示例。我没有在 Xcode 中对其进行测试,但这就是我的想法。只需相应地重建你的逻辑。
  • @RyanPoolos:你说得对,文本在单元格中右对齐,但实际上我希望我的文本在我使用的框架内右对齐。
  • 无论标签的框架是什么,它都会对齐。
【解决方案2】:

标签右对齐

yourLabel.textAlignment = NSTextAlignmentRight;

【讨论】:

    【解决方案3】:

    终于解决了我的问题。我犯了一个小错误

    [lisnerMessage setFrame:CGRectMake(75 ,20,size.width + 5,size.height+2)];
    

    我只是删除 size.width 并在文本右对齐之后给我的特定坐标 200。

    [lisnerMessage setFrame:CGRectMake(75 ,20,200,size.height+2)];
    

    感谢大家的回复

    【讨论】:

      【解决方案4】:

      你为什么不在界面生成器/故事板中制作标签并选择“右对齐”选项?然后将其连接为名为 lisnerMessage 和 lisnerMessage.text=[gMessageArray objectAtIndex:indexPath.row]; 的属性 这将显着减少您编写的代码量并且绝对有效。

      【讨论】:

        猜你喜欢
        • 2011-10-13
        • 2013-07-02
        • 2018-03-09
        • 1970-01-01
        • 2012-04-06
        • 1970-01-01
        • 2014-07-24
        • 2012-09-16
        • 1970-01-01
        相关资源
        最近更新 更多