28

我有一个UIButton,我正在尝试在其上设置标题和图像。

我想将 a 的标题对齐UIButton到左侧,并将图像对齐到右侧。我正在尝试获取时钟应用程序中计时器中按钮的外观和感觉(显示“计时器结束时”的按钮)。

我摆弄contentHorizontalAlignment,contentEdgeInsets和以实现我的目标titleEdgeInsetsimageEdgeInsets但无济于事。相同的文档也很稀疏。

我怎样才能达到同样的效果?

还有相关的问题,时钟应用程序中的计时器有两组文本,一组与图像左对齐,另一组右对齐?怎么能做到这一点UIButton?(虽然目前我不需要该功能)。

4

9 回答 9

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)];
于 2011-11-07T10:15:37.000 回答
35

更改上的imageEdgeInsets属性UIButton,然后使用setImage:forState:

于 2010-10-25T15:56:12.930 回答
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;
于 2012-07-10T15:38:05.880 回答
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];
于 2009-07-12T14:40:32.883 回答
4

您还可以覆盖titleRectForContentRect:imageRectForContentRect:方法,UIButton以便将文本和图像放置在您想要的任何位置。

于 2013-08-09T18:25:30.853 回答
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)
于 2016-01-12T08:27:28.470 回答
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)

    }
  }
}

斯威夫特 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)

        }
    }
}

我希望它能帮助像我这样的人!

于 2016-09-30T08:31:37.413 回答
1

这是 Swift 中的一个子类,UIButton它将左侧的图像和中心的文本对齐。设置imageLeftInset为您想要的值。

class LeftImageAlignedButton : UIButton {

var imageLeftInset : CGFloat = 10.0

override func layoutSubviews() {
    super.layoutSubviews()
    self.contentHorizontalAlignment = .Left
    if let font = titleLabel?.font {
        let textWidth = ((titleForState(state) ?? "") as NSString).sizeWithAttributes([NSFontAttributeName:font]).width
        let imageWidth = imageForState(state)?.size.width ?? 0.0
        imageEdgeInsets = UIEdgeInsets(top: 0, left: imageLeftInset, bottom: 0, right: 0)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: bounds.width/2 - textWidth/2.0 - imageWidth, bottom: 0, right: 0)
    }
}
}
于 2015-05-13T15:47:12.090 回答
-2

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

于 2012-03-01T14:03:52.673 回答