美文网首页
UIButton左文字右图片,上图片下文字

UIButton左文字右图片,上图片下文字

作者: heigo | 来源:发表于2017-12-28 11:54 被阅读0次
        UIImage *img = [UIImage imageNamed:@"hoVV"];
        [_categoryBtn setImage:img forState:UIControlStateNormal];
        _categoryBtn.backgroundColor = [UIColor greenColor];
        CGRect rect = [_categoryBtn.titleLabel.text boundingRectWithSize:CGSizeMake(100000, 50) options:NSStringDrawingUsesLineFragmentOrigin  attributes:@{NSFontAttributeName:[FGLiveTools getFontWithType:FGBook size:60]} context:nil];
        [_categoryBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -img.size.width, 0, img.size.width)];
        [_categoryBtn setImageEdgeInsets:UIEdgeInsetsMake(0,rect.size.width+20, 0, -rect.size.width)];
    

    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度,其它不边

    相关文章

      网友评论

          本文标题:UIButton左文字右图片,上图片下文字

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