UIButton如何正確調整imageView及titleLabel的位置

   UIImage *buttonImage = [UIImage imageNamed:@"daifukuan-ico"];orm

    

    CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage);it

    

    CGFloat buttonImageViewHeight = CGImageGetWidth(buttonImage.CGImage);scroll

    

    if ([UIScreen mainScreen].scale == 2.0f) {// iOS 4.0+im

        

        buttonImageViewWidth *= 0.8f;view

        

        buttonImageViewHeight *= 0.8f;vi

        

    }co

    

    NSString *buttonTitle = @"中華人";ab

    

    UIFont *buttonTitleFont = [UIFont systemFontOfSize:14.0f];tar

    

    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(140, 140)];

    

    [b setBounds:CGRectMake(0, 0, 90, 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;

    

    // 設置imageEdgeInsets

    

    CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y;

    

    CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x;

    

    CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop;

    

    CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft;

    

    b.imageEdgeInsets = UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);

    

    // 設置titleEdgeInsets

    

    CGFloat titleEdgeInsetsTop = endTitleLabelCenter.y-startTitleLabelCenter.y;

    

    CGFloat titleEdgeInsetsLeft = endTitleLabelCenter.x - startTitleLabelCenter.x;

    

    CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop;

    

    CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft;

    

    b.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);

相關文章
相關標籤/搜索