UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImage *buttonImage = [UIImage imageNamed:@"xl.png"];

    CGFloat buttonImageViewWidth = 50;//CGImageGetWidth(buttonImage.CGImage);

    CGFloat buttonImageViewHeight = 50;//CGImageGetHeight(buttonImage.CGImage);

    if ([UIScreen mainScreen].scale == 2.0f) {

        buttonImageViewWidth*=0.5f;

        buttonImageViewHeight*=0.5f;

    }

    NSString *buttonTitle = @"笑脸呵呵呵";

    UIFont *buttonTitleFont = [UIFont systemFontOfSize:17.0f];

    CGSize buttonTitleLabelSize = [buttonTitle sizeWithFont:buttonTitleFont];

     // button宽度,至少为imageView宽度与titleLabel宽度之和

    CGFloat buttonWidth = buttonImageViewWidth + buttonTitleLabelSize.width;

     // button高度,至少为imageView高度与titleLabel高度之和

    CGFloat buttonHeight = buttonImageViewHeight +buttonTitleLabelSize.height;

    UIButton *b = [[UIButton alloc] init];

    [b setCenter:CGPointMake(160, 160)];

    [b setBounds:CGRectMake(0, 0, buttonWidth, buttonHeight)];

    [b.titleLabel setFont:buttonTitleFont];

    [b setBackgroundColor:[UIColor redColor]];

    

    

    [b setImage:buttonImage forState:UIControlStateNormal];

    [b.imageView setBackgroundColor:[UIColor greenColor]];

    

    

    [b setTitle:buttonTitle forState:UIControlStateNormal];

    [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [b setTitleColor:[UIColor scrollViewTexturedBackgroundColor] forState:UIControlStateHighlighted];

    [b.titleLabel setBackgroundColor:[UIColor whiteColor]];

    [self.view addSubview:b];

    

    CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(b.bounds), CGRectGetMidY(b.bounds));

//    找出imageView 最终的center

    CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x,CGRectGetMidY(b.imageView.bounds));

//    找出titleLabel 最终的center

    CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetHeight(b.bounds)-CGRectGetMidY(b.titleLabel.bounds));

//    取得imageView最初的center

    CGPoint startimageViewCenter = b.imageView.center;

//    取得titleLabel最初的center

    CGPoint startTitleLabelCenter =b.titleLabel.center;

//    设置imageEdgelnsets

    CGFloat imageEdgelnsetsTop = endImageViewCenter.y - startimageViewCenter.y;

    CGFloat imageEdgelnsetsLeft = endImageViewCenter.x - startimageViewCenter.x;

    CGFloat imageEdgelnsetsBottom = -imageEdgelnsetsTop;

    CGFloat imageEdgelnsetRight = -imageEdgelnsetsLeft;

    b.imageEdgeInsets = UIEdgeInsetsMake(imageEdgelnsetsTop, imageEdgelnsetsLeft, imageEdgelnsetsBottom, imageEdgelnsetRight);

    

    

// 设置titleEdgeInsets

    CGFloat titleEdgelnsetsTop = endTitleLabelCenter.y -startTitleLabelCenter.y;

    CGFloat titleEdgelnsetsLeft = endTitleLabelCenter.x - startTitleLabelCenter.x;

    CGFloat titleEdgelnsetsBottom = -titleEdgelnsetsTop;

    CGFloat titleEdgelnsetsRight = -titleEdgelnsetsLeft;

    b.titleEdgeInsets = UIEdgeInsetsMake(titleEdgelnsetsTop, titleEdgelnsetsLeft, titleEdgelnsetsBottom, titleEdgelnsetsRight);

    

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值