UILabel文字与父视图边框出现混合渲染问题求助
图层渲染异常:UILabel文字与父视图边框颜色混合
我有一个不透明的UIView(TagView),内部包含白色不透明的UILabel,将其作为带边框视图(MyControl)的子视图显示时,发现UILabel的文字中出现了父视图的边框颜色(如字母"y"与边框颜色混合)。尝试让父视图其他内容重叠显示时渲染正常,仅边框存在此问题,推测是父视图边框与UILabel文字的图层渲染混合逻辑异常。
以下是相关代码:
import UIKit import PlaygroundSupport enum TagType { case minimal case rounded case circle } class TagView: UIView { var tagType: TagType = .rounded { didSet { updateUI() }} var isSelected = false { didSet { updateUI() }} private(set) lazy var label: UILabel = { let label = UILabel() label.textAlignment = .center label.translatesAutoresizingMaskIntoConstraints = false label.clipsToBounds = true label.font = .systemFont(ofSize: 15, weight: .semibold) return label }() override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { super.init(coder: coder) setup() } private var leadingLabelAnchor: NSLayoutConstraint? private var trailingLabelAnchor: NSLayoutConstraint? private var topLabelAnchor: NSLayoutConstraint? private var bottomLabelAnchor: NSLayoutConstraint? var constantValue: CGFloat { tagType == .circle ? 12 : 8 } var verticalInsetTop: CGFloat { tagType == .minimal ? 3 : 5 } var verticalInsetBottom: CGFloat { tagType == .minimal ? 3 : 7 } private func setup() { clipsToBounds = true addSubview(label) leadingLabelAnchor = label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: constantValue) trailingLabelAnchor = label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -constantValue) leadingLabelAnchor?.priority = .defaultHigh leadingLabelAnchor?.isActive = true trailingLabelAnchor?.priority = .defaultHigh trailingLabelAnchor?.isActive = true topLabelAnchor = label.topAnchor.constraint(equalTo: topAnchor, constant: verticalInsetTop) topLabelAnchor?.isActive = true bottomLabelAnchor = label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -verticalInsetBottom) bottomLabelAnchor?.isActive = true layer.borderColor = UIColor.blue.cgColor layer.borderWidth = 1 updateUI() } override func layoutSubviews() { super.layoutSubviews() updateUI() } private func updateUI() { leadingLabelAnchor?.constant = constantValue trailingLabelAnchor?.constant = -constantValue topLabelAnchor?.constant = verticalInsetTop bottomLabelAnchor?.constant = -verticalInsetBottom backgroundColor = isSelected ? .blue : .clear label.backgroundColor = backgroundColor label.textColor = isSelected ? .white : .blue switch tagType { case .minimal: layer.cornerRadius = 10 case .rounded: layer.cornerRadius = 12 case .circle: layer.cornerRadius = bounds.height / 2 } } func configure(with text: String) { label.text = text } } final class MyControl: UIControl { private lazy var tagView: TagView = { let view = TagView() return view }() override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setup() { tagView.translatesAutoresizingMaskIntoConstraints = false tagView.isSelected = true backgroundColor = .clear layer.cornerRadius = 10 layer.borderWidth = 2 layer.borderColor = UIColor.blue.cgColor addSubview(tagView) NSLayoutConstraint.activate( [ tagView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8), tagView.topAnchor.constraint(equalTo: topAnchor, constant: -16) ] ) tagView.configure(with: "7 dayyyyy") } } class MyViewController : UIViewController { override func loadView() { let view = UIView() view.backgroundColor = .white let label = UILabel() label.frame = CGRect(x: 150, y: 200, width: 200, height: 20) label.text = "Hello World!" label.textColor = .black let myControl = MyControl() myControl.translatesAutoresizingMaskIntoConstraints = false view.addSubview(myControl) NSLayoutConstraint.activate([ myControl.centerXAnchor.constraint(equalTo: view.centerXAnchor), myControl.centerYAnchor.constraint(equalTo: view.centerYAnchor), myControl.widthAnchor.constraint(equalToConstant: 300), myControl.heightAnchor.constraint(equalToConstant: 50), ]) view.addSubview(label) self.view = view } } // Present the view controller in the Live View window PlaygroundPage.current.liveView = MyViewController()
内容的提问来源于stack exchange,提问作者Evgenii Shishko
相关产品推荐
相关产品推荐

