之前做了个排序按钮是继承UIButton来实现的, 这次在UIbutton上添加 (高大上就是所谓的图文混排),主要还是EdgeInsets。
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CustomDropBtn : NSObject
+(UIButton *)DropBtnWithFrame:(CGRect) farme withTitle:(NSString *)title;
@end
#import "CustomDropBtn.h"
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
@implementation CustomDropBtn
+(UIButton *)DropBtnWithFrame:(CGRect) frame withTitle:(NSString *)title
{
NSDictionary *attributes=@{NSFontAttributeName: [UIFont systemFontOfSize:20.f],NSForegroundColorAttributeName:RGB(26, 26, 26)};
CGRect titleframe=[title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
UIButton *dropBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[dropBtn setFrame:frame];
[dropBtn setImage:[UIImage imageNamed:@"yzp_data_dropdown.png"] forState:UIControlStateNormal];
[dropBtn setImage:[UIImage imageNamed:@"yzp_data_dropback.png"] forState:UIControlStateSelected];
[dropBtn setImageEdgeInsets:UIEdgeInsetsMake((frame.size.height-5)/2, frame.size.width-20-8, (frame.size.height-5)/2, 20)];
[dropBtn setTitle:title forState:UIControlStateNormal];
[dropBtn setTitleColor:RGB(26, 26, 26) forState:UIControlStateNormal];
[dropBtn.titleLabel setFont:[UIFont systemFontOfSize:20.0]];
[dropBtn setTitleEdgeInsets:UIEdgeInsetsMake((frame.size.height-titleframe.size.height)/2,(frame.size.width-titleframe.size.width)/2-28, (frame.size.height-titleframe.size.height)/2,0)];
[dropBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
return dropBtn;
}
@end