【发布时间】:2011-09-28 12:07:47
【问题描述】:
我有一个标签添加到一个单元格,该单元格具有基于要添加到其中的文本的动态高度。我已将字体大小设置为 12,如下所示:
CGFloat height = [CustomCell getIndividualLabelHeight:text];
NSLog(@"height of commet:%@ is %f",commentText, height);
CustomOHAttributLabel *label = [[CustomOHAttributLabel alloc]initWithFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, 2*CELL_SPACING+totalCommentLabelHeight, CELL_CONTENT_WIDTH - (CELL_TEXT_LEFT_MARGIN*2), height)];
[label setLabelwithText:text fontSize:12 andSubString:userName withURL:url];
但是,在我的 getIndividualLabelHeight 方法中,如果我也将字体设置为 12.0(在设置 CGSize 大小时),标签中的文本可能会被截断。只有当我将它设置为 14 时,文本才不会被截断。
+ (CGFloat)getIndividualLabelHeight:(NSString *)text
{
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
//The full text will only show when I set fontsize to 14 (instead of 12)
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
有人知道为什么我不能在我的 get height 方法中设置与我用于文本的实际字体大小相同的字体大小吗?
我已经为 CustomOHAttributLabel 添加了我的实现代码以供进一步参考
@implementation CustomOHAttributLabel
- (CustomOHAttributLabel*) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
- (void) setLabelwithText:(NSString *)text fontSize:(CGFloat)fontSize andSubString:(NSString *)subString withURL:(NSString *)url
{
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:text];
[attrStr setFont:[UIFont systemFontOfSize:fontSize]];
[attrStr setTextColor:[UIColor grayColor]];
[attrStr setFont:[UIFont boldSystemFontOfSize:fontSize] range:[text rangeOfString:subString]];
[attrStr setTextColor:[UIColor darkGrayColor] range:[text rangeOfString:subString]];
self.attributedText = attrStr;
[self addCustomLink:[NSURL URLWithString:url] inRange:[text rangeOfString:subString]];
}
@end
【问题讨论】:
标签: objective-c ios size height uilabel