[英]How to add an image in the middle of UIButton title text?
I want to reach the following design: https://i.stack.imgur.com/D5kN3.png我想达到以下设计: https : //i.stack.imgur.com/D5kN3.png
How's it possible to set an image/icon in the middle of the button text?如何在按钮文本的中间设置图像/图标?
You can use an attributed string for that:您可以为此使用属性字符串:
// 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
You can use background image and set your labels right and left.您可以使用背景图像并左右设置标签。 Or you can create a new xib view, and extend UIButton as you wish and use it
或者您可以创建一个新的 xib 视图,并根据需要扩展 UIButton 并使用它
I see 2 possible options here:我在这里看到 2 个可能的选项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.