【问题标题】:UILabel not aligning perfectly center when wrapping包装时 UILabel 未完全居中对齐
【发布时间】:2010-06-20 08:07:38
【问题描述】:

例如看图片:http://img25.imageshack.us/img25/6996/90754687.png
灰色背景表示 UILabel 框架的大小。

由于某种原因,第一行换行文本似乎并不总是居中,即使我使用的是 UITextAlignmentCenter。

这是我用来设置标签的代码:

    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.font = [UIFont boldSystemFontOfSize:fontHeight];
    titleLabel.backgroundColor = [UIColor grayColor];
    titleLabel.numberOfLines = 2;
    titleLabel.lineBreakMode = UILineBreakModeMiddleTruncation;

    NSString * title = file.name;
    CGSize maximumSize = CGSizeMake(thumbnailWidth+4,fontHeight * 3);
    UIFont * font = [UIFont boldSystemFontOfSize:12];
    CGSize stringSize = [title sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:titleLabel.lineBreakMode];

    CGRect stringFrame = CGRectMake(0, thumbnailHeight + thumbnailPadding, thumbnailWidth + 4, stringSize.height);
    titleLabel.text = title;
    titleLabel.frame = stringFrame;
    titleLabel.textAlignment = UITextAlignmentCenter;

【问题讨论】:

  • 嗨@David Liu。您找到解决方案了吗?
  • @user4806509 我不记得了。很久以前了。

标签: iphone cocoa-touch ipad uikit


【解决方案1】:

是因为文本中没有空格吗?在 IB 中,如果你没有空格,它的反应似乎就像你得到的一样。将换行模式设置为换行往往会使第二行居中到第一行,但这也可能不是您想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-09
    • 2017-07-18
    • 2012-10-07
    • 2015-07-25
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多