[카테고리:] swift

  • UIView 하위의 모든 텍스트를 가져와서 삽입하는 코드

    class AccessibleButtonContainerView: UIView {
        override var accessibilityLabel: String? {
            get {
                let combinedLabels = subviews.compactMap { view -> String? in
                    if let label = view as? UILabel {
                        return label.text
                    }
                    return view.accessibilityLabel
                }.joined(separator: " ")
                
                return combinedLabels.isEmpty ? nil : combinedLabels
            }
            set {
                super.accessibilityLabel = newValue
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupAccessibility()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            setupAccessibility()
        }
        
        private func setupAccessibility() {
            isAccessibilityElement = true
            accessibilityTraits = .button
        }
    }