从0开始架构一个IOS程序——iOS 根据文字的长度来动态设置UILabel的大小

UIlabel根据文字的长度来动态设置的大小

#import <Foundation/Foundation.h>

@interface SINALabelUtils : NSObject


//根据文字内容来动态设置label的宽与高
//参数一 tagLabel 动态设置的label
//参数二 tagString 需要测量的文字的长度
+(UILabel *) updateLabel:(UILabel*) tagLabel andWithString:(NSString*)tagString;

//根据文字内容来动态设置label的宽与高
//参数一 tagLabel 动态设置的label
//参数二 tagString 需要测量的文字的长度
//参数三 fontSize  需要设置的文字的大小
+(UILabel *) updateLabel:(UILabel*) tagLabel andWithString:(NSString*)tagString andWithTextSize:(NSInteger) fontSize;
//根据文字内容来动态设置label的宽与高
//参数一 tagLabel 动态设置的label
//参数二 tagString 需要测量的文字的长度
//参数三 tagWidth  label 可设置的最大宽度
//参数四 tagHeight label 可设置的最大高度
//参数五 fontSize  需要设置的文字的大小
+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTagWidth:(NSInteger) tagWidth andWithTagHeight:(NSInteger) tagHeight andWithTagFontSize:(NSInteger)fontSize;
@end
#import "SINALabelUtils.h"

@implementation SINALabelUtils


+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString{
    return [[self class] updateLabel:tagLabel andWithString:tagString andWithTagWidth:[UIScreen mainScreen].bounds.size.width andWithTagHeight:[UIScreen mainScreen].bounds.size.height andWithTagFontSize:16];
}

+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTextSize:(NSInteger)fontSize{
    return [[self class] updateLabel:tagLabel andWithString:tagString andWithTagWidth:[UIScreen mainScreen].bounds.size.width andWithTagHeight:[UIScreen mainScreen].bounds.size.height andWithTagFontSize:fontSize];
}

+(UILabel *)updateLabel:(UILabel *)tagLabel andWithString:(NSString *)tagString andWithTagWidth:(NSInteger) tagWidth andWithTagHeight:(NSInteger) tagHeight andWithTagFontSize:(NSInteger)fontSize{

    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],};
    CGSize textSize = [tagString boundingRectWithSize:CGSizeMake(tagWidth, tagHeight) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
    tagLabel.numberOfLines=0;
    //重置label的frame 主要是重设label的宽度与调试
    tagLabel.frame=CGRectMake(tagLabel.frame.origin.x, tagLabel.frame.origin.y, textSize.width, textSize.height);
    return tagLabel;
}

@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早起的年轻人

创作源于分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值