宽高自适应的标签tips

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        [self createView];

    }

    return self;

}

- (void)createView{

    self.backgroundColor = [UIColor whiteColor];

    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 30)];

    self.titleLabel.textColor = [UIColor orangeColor];

    self.titleLabel.text = @"Knowledge";

    [self addSubview:self.titleLabel];

    ///

    self.line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 0.5)];

    [self addSubview:self.line];

    self.line.backgroundColor = [UIColor lightGrayColor];

    ///

    self.knowledgeScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50+0.5, self.frame.size.width, self.frame.size.height-(50+0.5))];

    [self addSubview:self.knowledgeScroll];

   NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"语文",@"的方化学阿化学阿发送到发送到发送到发送到式发顺丰阿发送到发送到发送到发送到式发顺丰数学数学",@"阿达",@"化学阿发送到发送到",@"物理搜索",@"生物",@"地理",@"的化学阿发送到发送到方式发顺化学阿发送到发送到化学阿发送到发送到丰数学",@"阿达",@"化学阿发送到发送到化学阿发送到发送化学阿发送到发送到到",@"物理搜索",@"生物",@"地理",@"的方式化学阿发送到发送到发顺丰数学",@"阿达",@"化化学化学阿发送到发送到阿发送到发送到学阿发送到发送到的方化学阿化学阿发送到发送到发送到发送到式发顺丰阿发送到发送到发送到发送到式发顺丰数学数学的方化学阿化学阿发送到发送到发送到发送到式发顺丰阿发送到发送到发送到发送到式发顺丰数学数学",@"物理搜索",@"生物",@"地理",@"的方式发顺丰数学",@"阿达",@"化学阿发送到发送化学阿发送到发送到到",@"物理搜索",@"生物",@"地理",@"阿达",@"化学阿发送到发送到",@"物理搜索",@"生物",@"地理", nil];

    [self refreshWithArr:arr];

    

}

- (void)refreshWithArr:(NSMutableArray *)arr{

        /// 标签与标签的间距

        CGFloat magin = 5;

        CGFloat y = 10;

        CGFloat x = 15;

        CGFloat current_w = 0;

        UIView *current = nil;

        if (arr.count <= 0) {

            

        }else {

            for (int i = 0; i < arr.count; i++) {

                

                NSString *str = arr[i];

                

                NSMutableDictionary *fontDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName,nil];

                /// 一行的标准高度

                CGFloat h1 = [@"aa" boundingRectWithSize:CGSizeMake(Width-30-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil].size.height;

                CGFloat w = 0;

                /// 先固定宽算高度

                CGFloat h = [str boundingRectWithSize:CGSizeMake(Width-30-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil].size.height;

                if (h>h1) {

                    /// 高于一行 高为h, 宽为 Width-30-20

                    w = Width-30-20;

                }else{

                    /// 不高于一行 固定高算宽

                    w = [str boundingRectWithSize:CGSizeMake(0, h1) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil].size.width;

                    ///高为h1, 宽为 w

                }

                if (Width < w+20+current_w+magin+x) {

                    /// 一行的标签宽和间隙总和大于屏幕宽,,进行换行

                    x = 15;

                    y = y+h+13+magin;

                }else{

                     /// 一行的标签宽和间隙总和不大于屏幕宽

                    if (i != 0) {

                        x = current.frame.origin.x+current.frame.size.width+magin;

                    }

                }

                UIView *view = [[UIView alloc] init];

                if (h>h1) {

                    // 多行显示

                    view.frame = CGRectMake(x, y-(h/h1-1)*h1, w+20, h + 13);

                }else{

                    // 一行显示

                    view.frame = CGRectMake(x, y, w+20, h + 13);

                }

                [self.knowledgeScroll addSubview:view];

                view.layer.cornerRadius = 5;

                view.layer.masksToBounds = YES;

                view.backgroundColor = [UIColor orangeColor];

                

                UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 6.5, w, h)];

                title.text = [NSString stringWithFormat:@"%@",str];

                title.textColor = [UIColor whiteColor];

                title.numberOfLines = 0;

                title.textAlignment = NSTextAlignmentLeft;

                [view addSubview:title];

                title.font = [UIFont systemFontOfSize:15];

                current = view;

                current_w = current.frame.origin.x+current.frame.size.width;

                

                CGFloat height = y;

                if (current.frame.size.height > 0) {

                    height = current.frame.origin.y+current.frame.size.height;

                }

                if (height+10 < self.frame.size.height) {

                    self.knowledgeScroll.contentSize = CGSizeMake(Width, self.frame.size.height-50);

                }else {

                    self.knowledgeScroll.contentSize = CGSizeMake(Width, height+10);

                }

            }

        }

}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值