首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >圆圈中的iOS标签

圆圈中的iOS标签
EN

Stack Overflow用户
提问于 2016-11-16 12:28:05
回答 6查看 10.6K关注 0票数 6

嗨,我试图用图层来画一个圆圈,然而,我的标签只显示数字,而不是我为标签创建的背景色或边框。这是为什么?这是我的密码:

代码语言:javascript
复制
let countLabel = UILabel()
countLabel.text = "5"
let size:CGFloat = 55.0
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height :  size)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
//countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor

countLabel.center = CGPoint(x:200.0,y: 200.0)
countLabel.translatesAutoresizingMaskIntoConstraints = false
self.topContainer.addSubview(countLabel)
countLabel.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true
countLabel.trailingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 10).isActive = true        

我想得到这样的东西。

然而,上面没有输出橙色的颜色。为什么?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-11-16 19:44:53

创建一个视图并使其成为一个圆圈,然后在其中添加标签。以下是代码:

代码语言:javascript
复制
let size:CGFloat = 36
        let someView = UIView()
        someView.translatesAutoresizingMaskIntoConstraints = false
        someView.layer.cornerRadius = size/2
        someView.backgroundColor = .orange
        someView.layer.borderColor = UIColor.white.cgColor
        someView.layer.borderWidth = 2

        self.topContainer.addSubview(someView)

        someView.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true
        someView.trailingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 10).isActive = true
        someView.heightAnchor.constraint(equalToConstant: size).isActive = true
        someView.widthAnchor.constraint(equalToConstant: size).isActive = true

        let countLabel = UILabel()
        countLabel.text = "5"
        countLabel.textColor = UIColor.white
        countLabel.textAlignment = .center
        countLabel.font = UIFont.systemFont(ofSize: 14.0)

        countLabel.translatesAutoresizingMaskIntoConstraints = false
        someView.addSubview(countLabel)

        countLabel.centerXAnchor.constraint(equalTo: someView.centerXAnchor).isActive = true
        countLabel.centerYAnchor.constraint(equalTo: someView.centerYAnchor).isActive = true
票数 -2
EN

Stack Overflow用户

发布于 2016-11-16 12:45:12

尝试添加以下代码:

代码语言:javascript
复制
countLabel.layer.cornerRadius = 0.5 * countLabel.bounds.size.width

而不是:

代码语言:javascript
复制
// remove the below code of line from your code 
countLabel.layer.cornerRadius = size / 2
票数 5
EN

Stack Overflow用户

发布于 2016-11-16 12:42:13

我猜你在这里偶然发现了一个窃听器。

如果我将基于代码的代码添加到游乐场中,它将输出“空图像”,而不是显示视图。

但是如果我用一个框架创建标签,它就能工作。

不起作用:

代码语言:javascript
复制
let countLabel = UILabel()
countLabel.frame = CGRect(x : 0.0,y : 0.0,width : size, height :  size)

不起作用:

代码语言:javascript
复制
let countLabel = UILabel()
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height :  size)

不起作用:

代码语言:javascript
复制
let countLabel = UILabel()
countLabel.sizeToFit()

工作:

代码语言:javascript
复制
let size:CGFloat = 55.0
let countLabel = UILabel(frame: CGRect(x : 0.0,y : 0.0,width : size, height :  size))

我操场上的完整代码:

代码语言:javascript
复制
import UIKit
let size:CGFloat = 55.0

let countLabel = UILabel(frame: CGRect(x : 0.0,y : 0.0,width : size, height :  size))
countLabel.text = "5"
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center

countLabel.font = UIFont.systemFont(ofSize: 24.0)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor

countLabel.translatesAutoresizingMaskIntoConstraints = false

结果:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40632207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档