UIButton标题和图像对齐查询

时间:2009-07-12 14:24:36

标签: iphone cocoa-touch uibutton

我有UIButton,我正在尝试设置标题和图片。

我想将UIButton的标题与左侧对齐,并将图像对齐到右侧。我试图在Timer in Clocks应用程序中显示按钮的外观(“当计时器结束时”)。

我摆弄contentHorizontalAlignmentcontentEdgeInsetstitleEdgeInsetsimageEdgeInsets来实现我的目标,但无济于事。同样的文档也很稀疏。

我怎样才能达到同样的目标?

还有相关问题,Clock in Clocks应用程序有两组文本,一组与左侧对齐,另一组与图像对齐?如何在UIButton中完成? (我现在不需要那个功能)。

9 个答案:

答案 0 :(得分:49)

以下是我用于此的代码:

//Right-align the button image
CGSize size = [[myButton titleForState:UIControlStateNormal] sizeWithFont:myButton.titleLabel.font];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -size.width)];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, myButton.imageView.image.size.width + 5)];

答案 1 :(得分:35)

更改imageEdgeInsets上的UIButton媒体资源,然后使用setImage:forState:

答案 2 :(得分:14)

以下代码有效:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonRect;
[button setTitle:@"A title" forState:UIControlStateNormal];
[button setImage:buttonImage forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0.0, WIDTH(button.titleLabel) + 10.0, 0.0, 0.0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

答案 3 :(得分:9)

请记住,UIButton继承自UIView,因此您可以像其他任何视图一样向其添加子视图。在你的情况下,你会创建一个按钮,然后在左侧添加一个UILabel,在右边添加一个UIImage:

// Create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// Now load the image and create the image view
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(/*frame*/)];
[imageView setImage:image];

// Create the label and set its text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/*frame*/)];
[label setText:@"Your title"];

// Put it all together
[button addSubview:label];
[button addSubview:imageView];

答案 4 :(得分:4)

您还可以覆盖titleRectForContentRect:的{​​{1}}和imageRectForContentRect:方法,以便将文字和图片放在任何您想要的位置。

答案 5 :(得分:4)

在Swift中,任何关心的人(间隔为10pt):

let size = (self.titleForState(UIControlState.Normal)! as NSString).sizeWithAttributes([NSFontAttributeName:self.titleLabel!.font])
let offset = (size.width + 10)
self.imageEdgeInsets = UIEdgeInsetsMake(0, offset, 0, -offset)
self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, self.imageView!.frame.size.width + 10)

答案 6 :(得分:4)

对于在iOS 9上使用,我在这个帖子中尝试了很多答案,但它们都不适合我。我找到了自己的答案如下:

class MyButton: UIButton {

  override func layoutSubviews() {
    super.layoutSubviews()

    self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

    if self.imageView?.image != nil {
      // Move icon to right side
      self.imageEdgeInsets = UIEdgeInsets(
        top: 0,
        left: self.bounds.size.width - self.imageView!.image!.size.width,
        bottom: 0,
        right: 0)

      // Move title to left side
      self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView!.frame.size.width + 8, 0, 0)

    }
  }
}

SWIFT 3:

class MyButton: UIButton {

    override func layoutSubviews() {
        super.layoutSubviews()

        self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left

        if self.imageView?.image != nil {
            // Move icon to right side
            self.imageEdgeInsets = UIEdgeInsets(
                top: 0,
                left: self.bounds.size.width - self.imageView!.image!.size.width,
                bottom: 0,
                right: 0)

            // Move title to left side
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView!.frame.size.width + 8, 0, 0)

        }
    }
}

我希望它会像我一样帮助那个人!

答案 7 :(得分:1)

这是Swift中Bootstrap.getExService().execute(new Runnable(){ @Override public void run() { post(API_CONTEXT + "/login", "application/json", (request,response) -> { //stuff happening }, new JsonTransformer()); } }); 的子类,它将左侧的图像与中心的文本对齐。将UIButton设置为您想要的值。

imageLeftInset

答案 8 :(得分:-2)

创建一个UIButton子类并覆盖其layoutSubviews方法以对齐文本&图像编程。或者使用https://github.com/stevestreza/BlockKit/blob/master/Source/UIView-BKAdditions.h之类的内容,以便您可以使用块实现layoutSubviews。