ios开发之--使用UILabel Category 计算UILabel内容大小

在此仅做记录,代码如下:

.h

#import <UIKit/UIKit.h>

@interface UILabel (BoundingSize)
- (CGSize)boundingRectWithSize:(CGSize)size;
@end

.m

#import "UILabel+BoundingSize.h"

@implementation UILabel (BoundingSize)
- (CGSize)boundingRectWithSize:(CGSize)size
{
    NSDictionary *attribute = @{NSFontAttributeName: self.font};
    
    CGSize retSize = [self.text boundingRectWithSize:size
                                             options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil].size;
    
    return retSize;
}

@end

 

下面代码可供测试:

// 字符串
    NSString *str = @"北国风光,千里冰封,万里雪飘。望长城内外,惟余莽莽;大河上下,顿失滔滔。山舞银蛇,原驰蜡象,欲与天公试比高。须晴日,看红装素裹,分外妖娆。江山如此多娇,引无数英雄竞折腰。惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。北国风光,千里冰封,万里雪飘。望长城内外,惟余莽莽;大河上下,顿失滔滔。山舞银蛇,原驰蜡象,欲与天公试比高。须晴日,看红装素裹,分外妖娆。江山如此多娇,引无数英雄竞折腰。惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。北国风光,千里冰封,万里雪飘。望长城内外,惟余莽莽;大河上下,顿失滔滔。山舞银蛇,原驰蜡象,欲与天公试比高。须晴日,看红装素裹,分外妖娆。江山如此多娇,引无数英雄竞折腰。惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。";
    
    // 初始化label
    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:label];
    
    // label获取字符串
    label.text = str;
    
    
    // label获取字体
    label.font = [UIFont systemFontOfSize:18.0f];
    
    // 根据获取到的字符串以及字体计算label需要的size
    CGSize size = [label boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0)];
    
    // 设置无限换行
    label.numberOfLines = 0;
    
    // 设置label的frame
    label.frame = CGRectMake(0.0f, 50.0f, size.width, size.height);

效果如下:

 

还有一种方法,话不多说,直接上代码:

.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface NSString (Extension)
-(CGFloat)heightWithWidth:(CGFloat)width font:(CGFloat)font;
@end

.m

#import "NSString+Extension.h"

@implementation NSString (Extension)
-(CGFloat)heightWithWidth:(CGFloat)width font:(CGFloat)font{
    UIFont * fonts = [UIFont systemFontOfSize:font];
    CGSize size  = CGSizeMake(width, 100000.0);
    NSDictionary * dict  = [NSDictionary dictionaryWithObjectsAndKeys:fonts,NSFontAttributeName ,nil];
    size = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size.height;
}
@end

调用:

UILabel *contentLabel = [[UILabel alloc]init];
contentLabel.numberOfLines = 0;
[self.view addSubview:_contentLabel];
CGFloat contentHeight = [contentLabel.text heightWithWidth:CGRectGetWidth(self.contentView.bounds) - 40 font:14];
_contentLabel.frame = CGRectMake(20, CGRectGetMaxY(self.view.frame) + 20, CGRectGetWidth(self.view.bounds) - 40, contentHeight);

 

最后贡献个工具类:

.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface GlobalUI : NSObject
+ (UIImageView *)createImageViewbgColor:(UIColor *)bgColor;
+ (UILabel *)createLabelFont:(CGFloat )fontsize titleColor:(UIColor *)titleColor bgColor:(UIColor *)bgColor;
+ (UIButton *)createButtonWithImg:(UIImage *)img title:(NSString *)title titleColor:(UIColor *)titleColor;
@end

.m

#import "GlobalUI.h"

@implementation GlobalUI

+ (UIImageView *)createImageViewbgColor:(UIColor *)bgColor {
    UIImageView * img = [[UIImageView alloc] init];
    img.backgroundColor = bgColor;
    return img;
}

+ (UILabel *)createLabelFont:(CGFloat )fontsize titleColor:(UIColor *)titleColor bgColor:(UIColor *)bgColor{
    UILabel * lab = [[UILabel alloc]init];
    lab.font = [UIFont systemFontOfSize:fontsize];
    lab.textColor = titleColor;
    lab.backgroundColor = bgColor;
    return  lab;
}
+ (UIButton *)createButtonWithImg:(UIImage *)img title:(NSString *)title titleColor:(UIColor *)titleColor{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:img forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn  setTitleColor:titleColor forState:UIControlStateNormal];
     return  btn;
}


@end

其实类别真的很方便,虽然不太灵活和有局限性,不过实际写代码过程中,真的很方便,还有很多用法,如果写的不好,或者有更好的方法,请在下方评论!

 

posted @ 2018-01-15 23:02  稻草人11223  阅读(851)  评论(0编辑  收藏  举报
返回顶部