标签自动排列 笔记

-(void)createBookingMarksWithArray:(NSArray *)booksArray

{

//booksArray 是标签数组

    UILabel *titleLB1=[[UILabel alloc]initWithFrame:CGRectMake(0, 130, DEF_SCREEN_WIDTH, 20)];

    titleLB1.font=[UIFont fontWithName:@"Helvetica-Bold" size:15];

    titleLB1.textAlignment=NSTextAlignmentCenter;

    titleLB1.text=@"已有标签";

    titleLB1.backgroundColor=[UIColor clearColor];

    [self addSubview:titleLB1];

    CGFloat allWidth=30;


    static int j=0;

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

    {

        NSString *booking=[booksArray objectAtIndex:i];


        

//DEF_SCREEN_WIDTH 为屏幕宽度 

        if ((allWidth+5+booking.length*25)<DEF_SCREEN_WIDTH)

        {

            //NSLog(@"%f",allWidth);

            UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(allWidth+5, titleLB1.bottom+20+30*j,booking.length*25, 25)];

            label.backgroundColor=[UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1];

            label.text=booking;

            label.layer.cornerRadius=6;

            label.layer.masksToBounds = YES;

            label.textAlignment=NSTextAlignmentCenter;

            label.font=[UIFont systemFontOfSize:13];

            label.textColor=[UIColor whiteColor];

            UIFont *font = [UIFont fontWithName:@"Arial" size:18];

            CGSize size = CGSizeMake(320,25);

            CGSize labelsize = [booking sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

            [label setFrame:CGRectMake(allWidth+5, titleLB1.bottom+20+30*j, labelsize.width, 25)];

            [self addSubview:label];

            allWidth=label.right+5;


            //NSLog(@"%f",allWidth);

        }

        else

        {

            ++j;

            self.contentSize=CGSizeMake(DEF_SCREEN_WIDTH, 260+j*30);

            allWidth=30;

            UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(allWidth+5, titleLB1.bottom+20+30*j,booking.length*25, 25)];

            label.backgroundColor=[UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1];

            label.text=booking;

            label.layer.cornerRadius=6;

            label.layer.masksToBounds = YES;

            label.textAlignment=NSTextAlignmentCenter;

            label.font=[UIFont systemFontOfSize:13];

            label.textColor=[UIColor whiteColor];

            UIFont *font = [UIFont fontWithName:@"Arial" size:18];

            CGSize size = CGSizeMake(320,25);

            CGSize labelsize = [booking sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

            [label setFrame:CGRectMake(allWidth+5, titleLB1.bottom+20+30*j, labelsize.width, 25)];

            [self addSubview:label];

            allWidth=label.right+5;

        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值