第一种:
@interface BaseButton : UIButton
Title:(NSString *)text NormalImage:(UIImage*)normalImage
SelectedImage:(UIImage *)selectedImage; @end
-(
UIButton*)initWithFrame:(CGRect)frameTitle:(NSString *)text NormalImage:(UIImage*)normalImage
SelectedImage:(UIImage *)selectedImage; @end
-(UIButton*)initWithFrame:(CGRect)frame
Title:(NSString *)text NormalImage:(UIImage*)normalImage
SelectedImage:(UIImage *)selectedImage{
createButton.frame = frame;
[createButton setImage:normalImage forState:UIControlStateNormal];
[createButton setImage:selectedImage forState:UIControlStateSelected];
createButton.imageEdgeInsets = UIEdgeInsetsMake(0, 14, 11,0);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素
[createButton setTitle:text forState:UIControlStateNormal];
createButton.titleLabel.font =
[UIFont systemFontOfSize:12];
createButton.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
[createButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[createButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//[createButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
createButton.titleEdgeInsets = UIEdgeInsetsMake(26,
-14, 0, 0);
return createButton;
Title:(NSString *)text NormalImage:(UIImage*)normalImage
SelectedImage:(UIImage *)selectedImage{
BaseButton *createButton = [UIButton buttonWithType:UIButtonTypeCustom];
createButton.frame = frame;
[createButton setImage:normalImage forState:UIControlStateNormal];
[createButton setImage:selectedImage forState:UIControlStateSelected];
createButton.imageEdgeInsets = UIEdgeInsetsMake(0, 14, 11,0);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素
[createButton setTitle:text forState:UIControlStateNormal];
createButton.titleLabel.font =
[UIFont systemFontOfSize:12];
createButton.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
[createButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[createButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//[createButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
createButton.titleEdgeInsets = UIEdgeInsetsMake(26,
-14, 0, 0);
return createButton;
}
---使用--
UIButton *button = [[BaseButton alloc] initWithFrame:CGRectMake(100, 100, 45, 45) Title:@"苹果" NormalImage:[UIImage imageNamed:@"tabbarItemImage.png"] SelectedImage:[UIImage imageNamed:@"tabbarItemImage_h.png"]];
[button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
----------------------------------------------
第二种:
#import <UIKit/UIKit.h>
@interface WButton : UIButton @end
@interface WButton : UIButton @end
////返回背景边界 (image
+ title)
//- (CGRect)backgroundRectForBounds:(CGRect)bounds{
//
//}
//- (CGRect)contentRectForBounds:(CGRect)bounds{
//
//}
//返回title边界
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
}
//返回image边界
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
+ title)
//- (CGRect)backgroundRectForBounds:(CGRect)bounds{
//
//}
//- (CGRect)contentRectForBounds:(CGRect)bounds{
//
//}
//返回title边界
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
}
//返回image边界
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(10, 5, 25, 25);
}
----使用---
UIButton *button1 = [[WButton alloc]initWithFrame:CGRectMake(100, 200,45,45)];
button1.backgroundColor = [UIColor blackColor];
[button1 setTitle:@"苹果" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button1.titleLabel.font =
[UIFont systemFontOfSize:12];
[button1 setImage:[UIImage imageNamed:@"tabbarItemImage.png"] forState:UIControlStateNormal];
//button1.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
button1.backgroundColor = [UIColor blackColor];
[button1 setTitle:@"苹果" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button1.titleLabel.font =
[UIFont systemFontOfSize:12];
[button1 setImage:[UIImage imageNamed:@"tabbarItemImage.png"] forState:UIControlStateNormal];
//button1.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
[
self.view addSubview:button1];--------------------------------
第三种: