码迷,mamicode.com
首页 > 其他好文 > 详细

UILabel 自适应宽高

时间:2016-08-27 12:41:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

#import <UIKit/UIKit.h>

@interface UILabel (UILabel_LabelHeighAndWidth)
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont*)font;

+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font;
@end
#import "UILabel+UILabel_LabelHeighAndWidth.h"

@implementation UILabel (UILabel_LabelHeighAndWidth)
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font // 自适应高
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}

+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {// 自适应宽
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = font;
    [label sizeToFit];
    return label.frame.size.width;
}
@end

例如:CGFloat width1 = [UILabel getWidthWithTitle:strzhengcahngshu font:cell.zhengchangLabel.font];

        cell.zhengchangshuLabel.frame = CGRectMake(46, 60, width1, 15);

        cell.zhengchangshuLabel.text = strzhengcahngshu;

 说明:记得引入头文件

#import "UILabel+UILabel_LabelHeighAndWidth.h"

UILabel 自适应宽高

标签:

原文地址:http://www.cnblogs.com/ljcgood66/p/5812616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!