gpt4 book ai didi

swift - 如何将 UIImage 放在 UILabel 前面 - Swift

转载 作者:行者123 更新时间:2023-11-28 08:55:34 24 4
gpt4 key购买 nike

我有一个 UILabel,说明用户来自哪里(位置),我想在文本中放置一个小图标。

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "geolocationIcon")
let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment)
let myString: NSMutableAttributedString = NSMutableAttributedString(string: location + " ")
myString.appendAttributedString(attachmentString)
otherUserLocationLabel.attributedText = myString

目前是这样显示的

http://s16.postimg.org/vsnd8v5g1/UILABEL.jpg

如何将图标放在文本前面?谢谢!

更新

我最终创建了一个自定义 UIButton,然后简单地添加了 UIImage 和 Text,如下所示。

            otherUserLocationButton.setImage(UIImage(named: "geolocationIcon"), forState: .Normal)
otherUserLocationButton.setTitle(" " + location, forState: .Normal)

最佳答案

    let label = UILabel(frame: CGRectMake(50, 50, 300, 21))
label.center = CGPointMake(160, 284)
[![label.textAlignment = NSTextAlignment.Center][1]][1]
label.text = "Location text"
label.layer.borderWidth = 0.5
label.layer.borderColor = UIColor.blueColor().CGColor

let image: UIImage = UIImage(named: "image_name")!

var bgImage: UIImageView?
bgImage = UIImageView(image: image)
bgImage!.frame = CGRectMake(0,0,20,20)

label.addSubview(bgImage!)

self.view.addSubview(label)

enter image description here

关于swift - 如何将 UIImage 放在 UILabel 前面 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33171593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com