gpt4 book ai didi

ios - HighlightedTextColor 在选中 tableviewcell 时更改 UILabel 的文本颜色

转载 作者:行者123 更新时间:2023-11-28 06:02:23 25 4
gpt4 key购买 nike

有很多示例可以在选中 UITableViewCell 时更改 UILabel 文本的颜色。但是所有示例都用以下几行解释

if cell.selected {
cell.txtLabel1.textColor = UIColor.redColor()
}else {
cell.txtLabel1.textColor = UIColor.blackColor()
}

但是设置 cell.txtLabel1.highlightedTextColor = UIColor.redColor() 非常简单。粗线会导致任何问题吗?为什么其他示例没有粗线实现?

如果不将 isHighlighted 设置为 UILabel,则在选择 UITableViewCell 时也会更改文本的颜色。下面粘贴代码以供引用

class MenuViewCell: UITableViewCell {

@IBOutlet weak var lblTitle:UILabel?
@IBOutlet weak var imgIcon:UIImageView?

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let selectedView = UIView()
selectedView.backgroundColor = UIColor(colorLiteralRed: 244.0/255.0, green: 244.0/255.0, blue: 245.0/255.0, alpha: 1)
selectedBackgroundView = selectedView
lblTitle?.highlightedTextColor = UIColor(colorLiteralRed: 224.0/255.0, green: 121.0/255.0, blue: 43.0/255.0, alpha: 1)
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)


// Configure the view for the selected state
}

}

最佳答案

Will bolded line cause any problem.

我没有发现在 UILabel 上使用这个属性有任何问题,如果你阅读这个属性的文档它指出:

This color is applied to the label automatically whenever the isHighlighted property is set to true.

more info here

这是最有效的问题,但当然用法会有所不同,如下所示:

UILabel.highlightedTextColor = UIColor.red

切换 isHighlighted

UILabel.isHighlighted = cell.isSelected

更新

要在设置 cell.isSelected 时自动更改 UILabel.isHighlighted,您可以子类化 UITableViewCell,如下所示:

class MyTableViewCell: UITableViewCell {

@IBOutlet var label = UILabel!

override var isSelected: Bool {
didSet {
label.isHighlighted = isSelected
}
}

}

关于ios - HighlightedTextColor 在选中 tableviewcell 时更改 UILabel 的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49230170/

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