自定义按钮(文字和图标)

来源:互联网 发布:数据库过滤器 编辑:程序博客网 时间:2024/05/21 19:46

自定义按钮,即自定义按钮中的文字和图标。

自定义一个继承自UIButton的类,重写父类的一些方法:

代码如下:

.h文件

#import <UIKit/UIKit.h>@interface SStiBtn : UIButton@end

.m文件

#import "SStiBtn.h"@implementation SStiBtn-(instancetype)initWithFrame:(CGRect)frame{    self=[super initWithFrame:frame];    if (self) {        self.imageView.contentMode=UIViewContentModeCenter;        self.titleLabel.textAlignment=NSTextAlignmentRight        ;        [self.titleLabel setTextColor:[UIColor purpleColor]];    }    return self;}//设置内部图标的frame-(CGRect)imageRectForContentRect:(CGRect)contentRect{    CGFloat imageY=0;    CGFloat imageW=self.height;    CGFloat imageH=self.height;    CGFloat imageX=self.width-imageW;    return CGRectMake(imageX, imageY, imageW, imageH);}//设置内部文字的frame-(CGRect)titleRectForContentRect:(CGRect)contentRect{    CGFloat titleX=0;    CGFloat titleY=0;    CGFloat titleH=self.height;    CGFloat titleW=self.width-self.imageView.width;    return CGRectMake(titleX, titleY, titleW, titleH);}@end
0 0
原创粉丝点击