简体   繁体   English

将 UIButton 标题右侧的图像与 imageEdgeInsets 和 titleEdgeInsets 对齐

[英]Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets

I would like to add an image on the right side of the title something like this我想在标题的右侧添加像这样的图像

|Button  [image]|
|title          |

And I am able to achieve this by this code我可以通过这段代码实现这一点

button.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0)
button.imageEdgeInsets = UIEdgeInsetsMake(0, 135, 0, 5)

But when the title is large, the title label and image intersect with each other.但是当标题很大时,标题标签和图像相互交叉。 How can I break the title label?我怎样才能打破标题标签?

try this, it will never intersect title and image with each other.试试这个,它永远不会与标题和图像相交。 and i created a button in storyboard in centre.我在中间的故事板中创建了一个按钮。 @IBOutlet weak var myButton: UIButton!

  let somespace: CGFloat = 10

  self.myButton.setImage(UIImage(named: "cross"), forState: UIControlState.Normal)

  self.myButton.imageEdgeInsets = UIEdgeInsetsMake(0, self.myButton.frame.size.width - somespace , 0, 0)

  print(self.myButton.imageView?.frame)

  self.myButton.titleEdgeInsets = UIEdgeInsetsMake(0, (self.myButton.imageView?.frame.width)! + somespace, 0, 10 )

在此处输入图片说明

and if you want multiline title for button change line break mode for button's title并且如果您想要按钮的多行标题更改按钮标题的换行符模式

self.myButton.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping

在此处输入图片说明

Swift 3.0斯威夫特 3.0

In swift 3.0 you can easily do it in interface builder.在 swift 3.0 中,您可以在界面构建器中轻松完成。

Select the UIButton -> Attributes Inspector -> go to size inspector and modify the image or title insets.选择 UIButton -> Attributes Inspector -> go to size inspector 并修改图像或标题插入。 and if you want image on button's right side simply select the force Right-to-left in semantic view utilities如果您想要按钮右侧的图像,只需在语义视图实用程序中选择强制Right-to-left

在此处输入图片说明 . .

This extension works for me:这个扩展对我有用:

Extension:扩展:

extension UIButton {
  func imageToRight() {
      transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
      titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
      imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
  }
}

Usage:用法:

button.imageToRight()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像 - Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets 调整titleEdgeInsets和imageEdgeInsets时,UIButton上的标题和文本之间的空格不正确 - The space between title and text on UIButton is not correct when adjusting titleEdgeInsets and imageEdgeInsets 自定义 UIButton imageEdgeInsets/titleEdgeInsets 不工作 - Custom UIButton imageEdgeInsets/titleEdgeInsets not working UIButton:如何使用 imageEdgeInsets 和 titleEdgeInsets 将图像和文本居中? - UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets? UIButton:如何使用imageEdgeInsets和titleEdgeInsets设置图像和文本? - UIButton: How to set image and text using imageEdgeInsets and titleEdgeInsets? 使用Autolayout设置UIButton titleEdgeInsets和imageEdgeInsets很慢 - Setting UIButton titleEdgeInsets and imageEdgeInsets is slow with Autolayout 如何使用 imageEdgeInsets 更改 UIButton 中图像的 tintColor? - How to change tintColor of image in UIButton with imageEdgeInsets? 右侧标题中的UIButton图像 - UIButton image on right title in center 为什么我的 UIButton 的标题在使用 titleEdgeInsets 时在中间被截断? - Why my UIButton's title is truncated in the middle while using titleEdgeInsets? UIButton的titleEdgeInsets毫无意义 - titleEdgeInsets for UIButton make no sense
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM