Objective c 目标C:计算单个标签高度的问题

Objective c 目标C:计算单个标签高度的问题,objective-c,ios,size,height,uilabel,Objective C,Ios,Size,Height,Uilabel,我将一个标签添加到一个单元格中,该单元格根据要添加的文本具有动态高度。我已将字体大小设置为12,如下所示: CGFloat height = [CustomCell getIndividualLabelHeight:text]; NSLog(@"height of commet:%@ is %f",commentText, height); CustomOHAttributLabel *label = [[CustomOHAttributLabel alloc]initWithFrame:CG

我将一个标签添加到一个单元格中,该单元格根据要添加的文本具有动态高度。我已将字体大小设置为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方法中设置与我用于文本的实际字体大小相同的字体大小吗

我已经为CustomOHattribute标签添加了实现代码,以供进一步参考

 @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
请尝试以下代码:-

CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];;
    commentsTextLabel.tag =50;
    [commentsTextLabel setNumberOfLines:0];
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    NSString *text=@"cakghaahsdlajsldjasdsa;dkas;dkasdkasdasp'dlasp'dlas'dlas'dlas'dlas'dlas'dlasdlasdlasdlasdlas'das'dlasdas";
    [commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
    [cell.contentView addSubview:commentsTextLabel];
    [commentsTextLabel release];
根据此代码,标签高度将动态调整。
如果要在表格视图单元格中添加此标签,请确保不要忘记将单元格高度设置为动态。

谢谢您的回复。你知道为什么我当前的方法不能正常工作吗(如果我在getheight方法中设置字体大小,标签字体大小相同?)