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

发布于 2024-11-18 08:45:53 字数 2001 浏览 1 评论 0原文

我在单元格中添加了一个标签,该单元格具有基于要添加到其中的文本的动态高度。我已将字体大小设置为 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;
}

任何人都知道为什么我不能在获取高度方法中设置与我用于文本的实际字体大小相同的字体大小?

我已添加 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

I have a label added to a cell which has a dynamic height based on the text to be added to it. I have set my font size to be 12 as seen below:

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];

However, in my getIndividualLabelHeight method, if I set the font to 12.0 as well (in setting of CGSize size), the text in the label might be truncated. It is only when I set it to 14 will the text not be truncated.

+ (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;
}

Anyone has any idea why I cannot set the same font size in my get height method as the actual font size I am using for my text?

I have added my implementation code for the CustomOHAttributLabel for further reference

 @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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-11-25 08:45:53

试试这个代码:-

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];

从这个代码中,您的标签高度将动态调整。
确保您是否在表格视图单元格中添加此标签,因此不要忘记使单元格高度也动态化。

try this code:-

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];

from this code you label height will adjust dynamically.
Make sure if you are adding this label in your table view cell so don't forgot to make the cell height dynamic also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文