lizhen0203

- (void)createDataSource{

    

    _dataSource = [[NSMutableArray alloc] initWithObjects:@"有意义有意义",@"额企鹅请问企鹅请问我",@"而且",@"额订单喔喔喔喔",@"维恩的",@"订单",@"方法个",@"仿佛",@"威锋网",@"方法",@"大大大", @"呃呃呃",@"鹅鹅鹅鹅鹅鹅饿",nil];

}

 

- (void)createButton{

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 127)];

    view.backgroundColor = [UIColor whiteColor];

    

    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    [view addGestureRecognizer:tapGesture];

    

    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 6, 50, 30)];

    titleLabel.text = @"热门搜索";

    titleLabel.font = [UIFont systemFontOfSize:12];

//    titleLabel.textColor = [KK_Utils hexStringToColor:@"636363"];

    [view addSubview:titleLabel];

    

    NSInteger lastWidth = 0;

    NSInteger count = 0;

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

        

        NSString * hotSearch = _dataSource[i];

        CGFloat width = [hotSearch boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingTruncatesLastVisibleLine attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil].size.width;

        

        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(11+lastWidth, 35 + (count*45), width+14, 30)];

        

        

        if (lastWidth+width+14 > self.view.frame.size.width - 10) {

            lastWidth = 0;

            count ++;

            if (count >= 2) {

                break; //超过两行  跳出循环

            }

            button.frame = CGRectMake(11+lastWidth, 35 + (count*45), width+14, 30);

            [button setTitle:_dataSource[i] forState:UIControlStateNormal];

            button.titleLabel.font = [UIFont systemFontOfSize:14];

            button.tag = i+100000;

            [button addTarget:self action:@selector(pushSearchView:) forControlEvents:UIControlEventTouchUpInside];

            lastWidth = width+14+button.frame.origin.x;

            

        }else{

            lastWidth = width+14+button.frame.origin.x;

            [button setTitle:_dataSource[i] forState:UIControlStateNormal];

            button.titleLabel.font = [UIFont systemFontOfSize:14];

            button.tag = i+100000;

            [button addTarget:self action:@selector(pushSearchView:) forControlEvents:UIControlEventTouchUpInside];

        }

        

        [view addSubview:button];

    }

    

    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, 7)];

    [view addSubview:footView];

    [self.view addSubview:view];

}

 

分类:

技术点:

相关文章: