iOS

How To ?

日常整理吧算是,常用但是耗时或者容易忘记的。

Posted by Asingers Blog on January 21, 2019

For 循环创建Button等控件

1
2
3
4
5
6
7
配置:
#define Start_X          30.0f      // 第一个按钮的X坐标
#define Start_Y          60.0f     // 第一个按钮的Y坐标
#define Width_Space      33.0f      // 2个按钮之间的横间距
#define Height_Space     20.0f     // 竖间距
#define Button_Height   22.0f    // 高
#define Button_Width    84.0f    // 宽

创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   for (int i =0; i<self.buttonTitleArray.count; i++) {
    NSInteger index = i % 3;
    NSInteger page = i / 3;
    UIButton *itemBtn = [[UIButton alloc] init];
    itemBtn.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, 
    page  * (Button_Height + Height_Space)+Start_Y, 
    Button_Width, Button_Height);
    [itemBtn setTitle:self.buttonTitleArray[i] forState:UIControlStateNormal];
    [itemBtn setTitleColor:FONTCOLORDEEPGRAY forState:UIControlStateNormal];
    itemBtn.titleLabel.font = UIFONT(14);
    [itemBtn addTarget:self action:@selector(itemBtnClick:) 
    forControlEvents:UIControlEventTouchUpInside];
    itemBtn.tag = 200 +i;
    [itemBtn setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
    itemBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    itemBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    CGFloat spacing = 5;
    itemBtn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
    itemBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
    [self.contentView addSubview:itemBtn];
}
//多选
	- (void)itemBtnClick:(UIButton*)btn
{
	btn.selected = !btn.selected;
	if (btn.isSelected) {
    	[btn setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
	} else {
    	[btn setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
	}
	if (self.ButtonBlock) {
    	self.ButtonBlock(btn);
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//单选
- (void)itemBtnClick:(UIButton*)btn
{
	//将点击的选中,其余置为未选中
    for (int i = 0; i<self.buttonTitleArray.count; i++) {
        if (btn.tag == 200+i) {
            btn.selected = YES;
            [btn setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
            continue; //结束当前循环,继续下次
        }
        UIButton *bt =(UIButton *)[self.contentView viewWithTag:200+i];
        bt.selected = NO;
        [bt setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
    }
    if (self.ReasonButtonBlock) {
        self.ReasonButtonBlock(btn);
    }
}

根据文字长度布局循环Button

例如历史搜索这种UI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define LEFTGAP 18
#define COLUMNGAP 10
#define ROWGAP 10
#define TAGHEIGHT 30

-(void)setTagArray:(NSMutableArray *)tagArray{
_tagArray = tagArray;
for (UIView *tempView in self.contentView.subviews) {
    if ([tempView isKindOfClass:[UIButton class]]) {
        [tempView removeFromSuperview];
    }
}
CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
CGFloat h = 0;//用来控制button距离父视图的高
if (_tagArray.count == 0) {
    return;
}
for (int i = 0; i < self.tagArray.count; i++) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = 100 + i;
    button.backgroundColor = SPLITCOLOR;
    button.layer.cornerRadius = 4;
    [button addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:GRAYCOLOR1 forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    NSString *tempStr = self.tagArray[i];
    button.searchString = tempStr; //搜索词
    [button setTitle:tempStr.length>10?[NSString stringWithFormat:@"%@...",
    [tempStr substringToIndex:10]]:tempStr forState:UIControlStateNormal];
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGFloat length = [tempStr.length>10?[NSString stringWithFormat:@"%@...",
    [tempStr substringToIndex:10]]:tempStr 
    boundingRectWithSize:CGSizeMake(MAXFLOAT, TAGHEIGHT) 
    options:NSStringDrawingUsesLineFragmentOrigin 
    attributes:attributes context:nil].size.width;
    //设置button的frame
    if (button.tag ==100) {
        //具体业务,例如加图片
        if ([self.hotOrHistory isEqualToString:@"history"]) {
            button.frame = CGRectMake(LEFTGAP + w, h, length + 10+18 , TAGHEIGHT);
        }else if([self.hotOrHistory isEqualToString:@"hot"]){
            [button setImage:[UIImage imageNamed:@"hotFire"] forState:UIControlStateNormal];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(0, button.imageView.right+6, 0, 0)];
            button.frame = CGRectMake(LEFTGAP + w, h, length + 20+18 , TAGHEIGHT);
        }
    }else{
        button.frame = CGRectMake(10 + w, h, length + 10+18 , TAGHEIGHT);
    }
    
    //当button的位置超出屏幕边缘时换行 只是button所在父视图的宽度
    if(LEFTGAP + w + length + ROWGAP > ScreenW-18-18){
        w = 0; //换行时将w置为0
        h = h + button.frame.size.height + 10;//距离父视图也变化
        button.frame = CGRectMake(LEFTGAP + w, h, length + 10+18 , 30);//重设button的frame
    }
    if (h >=70) {
        // 控制最大高度
        return;
    }
    w = button.frame.size.width + button.frame.origin.x;
    [self.contentView addSubview:button];
	}
}
文章使用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议