自定义带图片和文字的Button的排版--陈棚

自定义button,动态加载图片与文字

 [footView addSubview:btnAllChoose];
    [btnAllChoose setTitle:str forState:UIControlStateNormal];
    [btnAllChoose setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    btnAllChoose.titleLabel.font = [UIFont systemFontOfSize:10];
    [btnAllChoose setImage:[UIImage imageNamed:@"list_selected_icon"] forState:UIControlStateSelected];
    [btnAllChoose setImage:[UIImage imageNamed:@"list_give_icon"] forState:UIControlStateNormal];
    [btnAllChoose addTarget:self action:@selector(btnAllChooseAction:) forControlEvents:UIControlEventTouchUpInside];
    btnAllChoose.tag = 1001;

自定义UIbutton并继承Uibutton,修改button,内部方法

// 内部图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = contentRect.size.height;
    CGFloat imageH = contentRect.size.height ;
//    DLOG(@"imageH=%g",imageH);
    return CGRectMake(0, 0, imageW, imageH);
}

// 内部文字的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    
    CGFloat titleW = contentRect.size.width-contentRect.size.height-5;
    CGFloat titleH = contentRect.size.height;
    return CGRectMake(titleH+5, 0, titleW, titleH);
}

 

posted @ 2015-04-30 13:07  sixindev  阅读(482)  评论(2编辑  收藏  举报