调整UILabel的大小以适应字体大小和文本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整UILabel的大小以适应字体大小和文本相关的知识,希望对你有一定的参考价值。
This code will resize a UILabel so that the label is as small as it can be in both width and height, based on the text in it and the font used.
frame = CGRectMake(160, labelTitle2.frame.origin.y + labelTitle2.frame.size.height + 136, 270, 25); UILabel *labelContent3 = [[UILabel alloc] initWithFrame: frame]; labelContent3.text = @"click here."; labelContent3.textColor = UIColorFromRGB(0x606060); labelContent3.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13]; labelContent3.backgroundColor = [UIColor clearColor]; labelContent3.textAlignment = UITextAlignmentLeft; labelContent3.numberOfLines = 0; maximumLabelSize = CGSizeMake(270,9999); expectedLabelSize = [labelContent3.text sizeWithFont:labelContent3.font constrainedToSize:maximumLabelSize lineBreakMode:labelContent3.lineBreakMode]; newFrame = labelContent3.frame; newFrame.size.height = expectedLabelSize.height; labelContent3.frame = newFrame; [cell.contentView addSubview:labelContent3]; [labelContent3 release];
以上是关于调整UILabel的大小以适应字体大小和文本的主要内容,如果未能解决你的问题,请参考以下文章