关于做一个自定义的button,图片和文字在一起的

来源:互联网 发布:人不知而不愠的知 编辑:程序博客网 时间:2024/05/16 07:33

//

//  ChoiceBtn.h

//  Guitar

//

//  Created by tt1 on 15/11/29.

//  Copyright © 2015 Ji Miao. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ChoiceBtn : UIButton


@end


//

//  ChoiceBtn.m

//  Guitar

//

//  Created by tt1 on 15/11/29.

//  Copyright © 2015 Ji Miao. All rights reserved.

//


#import "ChoiceBtn.h"


@implementation ChoiceBtn

- (id)initWithFrame:(CGRect)frame


{

    

    self = [superinitWithFrame:frame];

    

    if (self) {

        

        //可根据自己的需要随意调整

        

        self.titleLabel.textAlignment=NSTextAlignmentLeft;

        

        self.titleLabel.font=TTCommonFont(14);

        

        

        self.imageView.contentMode=UIViewContentModeRight;

        

    }

    

    returnself;

    

}


//更具buttonrect设定并返回文本labelrect

- (CGRect)titleRectForContentRect:(CGRect)contentRect


{

    

    CGFloat titleW = contentRect.size.width;

    

    CGFloat titleH = contentRect.size.height;

 

    

    CGFloat titleX = 0;

    

    CGFloat titleY = 0;

    

    

    

    contentRect = (CGRect){{titleX,titleY},{titleW,titleH}};

    

    return contentRect;

    

}




//更具buttonrect设定并返回UIImageViewrect


- (CGRect)imageRectForContentRect:(CGRect)contentRect


{

    

    CGFloat imageW = 8;

    

    CGFloat imageH = 8;

    

    CGFloat imageX = contentRect.size.width-8;

    

    CGFloat imageY = 10;

    

    

    

    contentRect = (CGRect){{imageX,imageY},{imageW,imageH}};

    

    return contentRect;

    

}


@end

这是自己亲自写的代码,大家可以试试写个自己的demo
0 0
原创粉丝点击