gpt4 book ai didi

iOS - 将角标(Badge)添加到 UITableViewCell

转载 作者:技术小花猫 更新时间:2023-10-29 10:36:47 25 4
gpt4 key购买 nike

如何向 UITableViewCell 添加角标(Badge),如下所示:

alt text http://img17.imageshack.us/img17/9974/img0001ac9.png

我应该简单地添加一个带有文本和标签的 subview 吗?

最佳答案

这是对@POF 回答的快速改进。我们不需要那么多 subview ,我们可以使用数学来支持 N 位数字,而不仅仅是 1-3:

func setDiscountBadge(count: Int) {
let size: CGFloat = 26
let digits = CGFloat( count("\(number)") ) // digits in the label
let width = max(size, 0.7 * size * digits) // perfect circle is smallest allowed
let badge = UILabel(frame: CGRectMake(0, 0, width, size))
badge.text = "\(number)"
badge.layer.cornerRadius = size / 2
badge.layer.masksToBounds = true
badge.textAlignment = .Center
badge.textColor = UIColor.whiteColor()
badge.backgroundColor = cfg.UIColors.brand
YOURCELL.accessoryView = badge // !! change this line
}

结果(我使用品牌颜色,但您可以使用任何颜色):

uilabel badge

关于iOS - 将角标(Badge)添加到 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/731185/

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