【发布时间】:2019-02-09 20:35:22
【问题描述】:
我想达到以下设计:https://i.stack.imgur.com/D5kN3.png
如何在按钮文本中间设置图像/图标?
【问题讨论】:
我想达到以下设计:https://i.stack.imgur.com/D5kN3.png
如何在按钮文本中间设置图像/图标?
【问题讨论】:
您可以为此使用属性字符串:
// create a NSMutableAttributedString with the text before the image
let beginning = NSMutableAttributedString(string: "Beginning")
// create a NSTextAttachment with the image
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "image.png")
// create an NSMutableAttributedString with the image
let imageString = NSAttributedString(attachment: imageAttachment)
// add the image to the string
beginning.append(imageString)
// create an NSMutableAttributedString with the text after the image
let end = NSAttributedString(string: "End")
beginning.append()
// set the attributed text of the label
label.attributedText = beginning
【讨论】:
您可以使用背景图片并左右设置标签。或者您可以创建一个新的 xib 视图,并根据需要扩展 UIButton 并使用它
【讨论】:
我在这里看到 2 个可能的选项:
【讨论】: