1. 程式人生 > >iOS 橫向展示瀑布流 排列不規則長度標題的UIButton

iOS 橫向展示瀑布流 排列不規則長度標題的UIButton

iOS,橫向排列不規則長度標題的UIButton

NSMutableArray *testArray = [NSMutableArray array];
    [testArray addObject:@"看家"];
    [testArray addObject:@"智慧硬一"];
    [testArray addObject:@"哇建立技術開發是"];
    [testArray addObject:@"技術"];
    [testArray addObject:@"索朗多"];
    [testArray addObject:@"科技"];
    [testArray addObject:@"索朗多索朗多"];
    [testArray addObject:@"科技"];
    [testArray addObject:@"上課福建省地方"];
    [testArray addObject:@"科技"];

    CGFloat startX = 10;
    CGFloat startY = 100;
    CGFloat buttonHeight = 40;
    
    for(int i = 0; i < 10; i++)
    {
        UIButton *btn = [[UIButton alloc]init];
        
        btn.backgroundColor = [UIColor whiteColor];
        btn.layer.cornerRadius = 5;
        [btn setTitle:testArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        
        CGSize titleSize = [testArray[i] sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:btn.titleLabel.font.fontName size:btn.titleLabel.font.pointSize]}];
        
        titleSize.height = 20;
        titleSize.width += 20;
    
        if(startX + titleSize.width > [UIScreen mainScreen].bounds.size.width){
            startX = 10;
            startY = startY + buttonHeight + 10;
        }
        btn.frame = CGRectMake(startX, startY, titleSize.width, buttonHeight);
        startX = CGRectGetMaxX(btn.frame) + 10;
    }


最後實現效果如下圖: