カスタムButton画像は上の文字が下にあります

2461 ワード

#import "buttonCustom.h"
#define KimageW 30
#define KtitleH 21
#define Kpadding 10
@interface buttonCustom()

@end

@implementation buttonCustom

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

//set title
- (void)setTitle:(NSString *)title forState:(UIControlState)state{
    [super setTitle:title forState:state];
   
}

//set image
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [super setImage:image forState:state];
    
}


//custom title rect
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    return CGRectMake(0 , Kpadding *2 , self.bounds.size.width, 21 );
}

//custom image rect
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    return CGRectMake(0 , Kpadding ,self.bounds.size.width , KimageW);
}
@end

 
転載先:https://www.cnblogs.com/Mantis-man/p/5104656.html