简体   繁体   English

在Swift中为Image添加标签

[英]Add label to Image in Swift

Im new to swift and working on app which displays number of unread messages like below image within the app 我是swift的新手并且正在使用应用程序,该应用程序在应用程序中显示未读消息的数量,如下图

在此输入图像描述

Counter will increase/decreases as the new messages gets added/read 随着新消息的添加/读取,计数器将增加/减少

In order to display that, I have Image with mail icon and wanted to add that green label as badge which shows the number of unread messages 为了显示它,我有图像与邮件图标,并希望添加绿色标签作为徽章,显示未读邮件的数量

I was thinking to add circular label to image but couldnt figure out how to add that or find any references. 我正在考虑为图像添加圆形标签,但无法弄清楚如何添加或查找任何引用。 Please assist 请协助

I was thinking to add circular label to image but couldnt figure out how to add that or find any references. 我正在考虑为图像添加圆形标签,但无法弄清楚如何添加或查找任何引用。 Please assist. 请协助。

Welcome to Stackoverflow. 欢迎来到Stackoverflow。 There are lots of resources out there on how to round a view, like: https://www.appcoda.com/ios-programming-circular-image-calayer/ 关于如何对视图进行舍入有很多资源,例如: https//www.appcoda.com/ios-programming-circular-image-calayer/

You are correct, one way to do what you want is to add a circular label. 你是对的,一种做你想要的方法是添加一个圆形标签。 That's it. 而已。 Now how to add a circular label? 现在如何添加圆形标签? You round the corner of your view by giving its cornerRadius the half of its height . 通过给它的cornerRadius提供其height的一半来绕过视图的角落。

Position the label to your desired position (with constraints) referencing your message icon. 将标签定位到您所需的位置(带约束),引用您的消息图标。 For example: 例如:

在此输入图像描述

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Corner radius to 25 height / 2
        self.label.layer.cornerRadius = 12.5
        self.label.clipsToBounds = true

        // Border
        self.label.layer.borderWidth = 2
        self.label.layer.borderColor = UIColor.black.cgColor

    }
}

Result: 结果:

在此输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM