美文网首页
7.21按钮的文字在图片正下方,循环创建

7.21按钮的文字在图片正下方,循环创建

作者: 小码农杰哥 | 来源:发表于2017-07-21 18:35 被阅读0次

    for (int i =0; i<2; i++) {

    _btn=[[UIButton alloc] initWithFrame:CGRectMake(((SCREEN_WIDTH-260*SCREEN_WIDTH/375)/2+1)*i, 0, (SCREEN_WIDTH-260*SCREEN_WIDTH/375)/2, 49)];

    [_btn setTitle:_titleArray[i]  forState:UIControlStateNormal];

    //            [btn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.imagebtnAry[i]]]]] forState:UIControlStateNormal];

    [_btn setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];

    [_btn setTitleColor:[UIColor colorWithHexString:@"#9c9c9c"] forState:UIControlStateNormal];

    [_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    _btn.titleLabel.font=[UIFont systemFontOfSize:10];

    _btn.tag = i;

    if (_btn.tag == 0) {

    [_btn setImage:[UIImage imageNamed:@"未收藏"] forState:UIControlStateNormal];

    [_btn setImage:[UIImage imageNamed:@"收藏"] forState:UIControlStateSelected];

    NSLog(@"++++++shoucang");

    }

    CGFloat totalHeight = (_btn.imageView.frame.size.height + _btn.titleLabel.frame.size.height);

    _btn.backgroundColor = [UIColor whiteColor];

    //设置按钮的图片和文字的

    _btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示

    [_btn setTitleEdgeInsets:UIEdgeInsetsMake(1.3*_btn.imageView.frame.size.height ,-_btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变

    [_btn setImageEdgeInsets:UIEdgeInsetsMake(-1.3*(totalHeight - _btn.imageView.frame.size.height), 0.0,0.0, -_btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少文字的宽度,其它不边

    [self.bottomView addSubview:_btn];

    }

    相关文章

      网友评论

          本文标题:7.21按钮的文字在图片正下方,循环创建

          本文链接:https://www.haomeiwen.com/subject/fagekxtx.html