【发布时间】: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