UIButton 左文右图,上文下图 ,上图下文

1.设置左文右图时 button.titleLabel.intrinsicContentSize.width +   button.imageView.bounds.size.width 值要小于button.size.width

2.设置上图下文或者上图下文时  button.titleLabel.intrinsicContentSize.height +   button.imageView.bounds.size.height  值要小于button.size.height

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [buttonsetFrame:CGRectMake(100,200,134,55)];

    button.backgroundColor = [UIColor grayColor];

    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];

    [buttonsetTitleColor:[UIColor  purpleColor] forState:UIControlStateNormal];

    [buttonsetTitle:@"马甲哥" forState:UIControlStateNormal];

    [buttonsetImage:[UIImage imageNamed:@"uc_novice_college_taskicon"] forState:UIControlStateNormal];

    button.titleLabel.backgroundColor = [UIColor yellowColor];

    [self.viewaddSubview:button];


    //左文右图

    /*

    CGFloat space = 4;

    CGSize buttonSize = button.frame.size;

    CGSize titleSize = button.titleLabel.intrinsicContentSize;

    CGSize imageSize = button.imageView.bounds.size;

    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageSize.width - space/2, 0, imageSize.width + space/2)];

    [button setImageEdgeInsets:UIEdgeInsetsMake(0, titleSize.width + space/2, 0, -titleSize.width - space/2)];

     */


    // 上图下文

    /*

    CGFloat space = 4;

    CGSize buttonSize = button.frame.size;

    CGSize titleSize = button.titleLabel.intrinsicContentSize;

    CGSize imageSize = button.imageView.bounds.size;

    CGFloat afterTop = (buttonSize.height - (titleSize.height + imageSize.height + space))/2;

    CGFloat afterLeft = (buttonSize.width - (titleSize.width + imageSize.width))/2;

    CGFloat imageTop = (buttonSize.height - imageSize.height)/2 - afterTop;

    CGFloat imageLeft = buttonSize.width/2 - afterLeft - imageSize.width/2;

    CGFloat titleTop = (buttonSize.height - titleSize.height)/2 - afterTop;

    CGFloat titleLeft = buttonSize.width/2 - afterLeft - titleSize.width/2;

    [button setImageEdgeInsets:UIEdgeInsetsMake(-imageTop , imageLeft, imageTop, -imageLeft)];

    [button setTitleEdgeInsets:UIEdgeInsetsMake(titleTop, -titleLeft, -titleTop, titleLeft)];

     */


    // 上文下图

    /*

    CGFloat space = 4;

    CGSize buttonSize = button.frame.size;

    CGSize titleSize = button.titleLabel.intrinsicContentSize;

    CGSize imageSize = button.imageView.bounds.size;

    CGFloat afterTop = (buttonSize.height - (titleSize.height + imageSize.height + space))/2;

    CGFloat afterLeft = (buttonSize.width - (titleSize.width + imageSize.width))/2;

    CGFloat imageTop = (buttonSize.height - imageSize.height)/2 - afterTop;

    CGFloat imageLeft = buttonSize.width/2 - afterLeft - imageSize.width/2;

    CGFloat titleTop = (buttonSize.height - titleSize.height)/2 - afterTop;

    CGFloat titleLeft = buttonSize.width/2 - afterLeft - titleSize.width/2;

    [button setImageEdgeInsets:UIEdgeInsetsMake(imageTop , imageLeft, -imageTop, -imageLeft)];

    [button setTitleEdgeInsets:UIEdgeInsetsMake(-titleTop, -titleLeft, titleTop, titleLeft)];

     */


    NSLog(@"titleEdgeInsets:%@",NSStringFromUIEdgeInsets(button.titleEdgeInsets));

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(UIButton 左文右图,上文下图 ,上图下文)