Lable自適應高度方法的封裝
ios7---Lable自適應高度封裝
+(UILabel )getInfoLabel:(UILabel )label withText:(NSString )ktext withFont:(CGFloat )kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor )kbackgroundColor
{
//內容顯示 高度自適應
CGSize sizeToFit =[ktext sizeWithFont:[UIFont systemFontOfSize:kfont]constrainedToSize:CGSizeMake(krect.size.width,10000)lineBreakMode:NSLineBreakByWordWrapping];CGRect labelframe = CGRectMake(krect.origin.x,krect.origin.y,sizeToFit.width, sizeToFit.height); label.frame=labelframe; label.numberOfLines=0; label.lineBreakMode=NSLineBreakByTruncatingTail; label.font=[UIFont systemFontOfSize:kfont]; label.text = ktext; label.backgroundColor=kbackgroundColor; label.textAlignment = NSTextAlignmentLeft; label.textColor = [UIColor blackColor]; return label; } ios8---Lable自適應高度封裝 +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor { label.numberOfLines =0; UIFont * tfont =kfont; label.font = tfont; label.lineBreakMode =NSLineBreakByTruncatingTail ; label.text =ktext; //高度估計文本大概要顯示幾行,寬度根據需求自己定義。 MAXFLOAT 可以算出具體要多高 CGSize size =CGSizeMake(krect.size.width,10000); NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil]; //ios7方法,獲取文本需要的size,限制寬度 CGSize actualsize =[ktext boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size; label.frame=CGRectMake(krect.origin.x,krect.origin.y,actualsize.width, actualsize.height); label.backgroundColor=kbackgroundColor; return label; } </pre>
本文由用戶 nwbg 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!