Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/Folium-iOS/Classes/PaddedBlurredLabel.swift
2 views
//
//  PaddedBlurredLabel.swift
//  Folium-iOS
//
//  Created by Jarrod Norwell on 7/9/2025.
//

import Foundation
import UIKit

class PaddedBlurredLabel : UIView {
    var visualEffectView: UIVisualEffectView? = nil
    
    var label: UILabel? = nil
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        layer.cornerCurve = .continuous
        
        visualEffectView = if #available(iOS 26, *) {
            .init(effect: UIGlassEffect(style: .regular))
        } else {
            .init(effect: UIBlurEffect(style: .systemMaterial))
        }
        guard let visualEffectView else {
            return
        }
        visualEffectView.translatesAutoresizingMaskIntoConstraints = false
        insertSubview(visualEffectView, belowSubview: self)
        
        label = .init()
        guard let label else {
            return
        }
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .bold(.footnote)
        label.textAlignment = .center
        label.textColor = .label
        addSubview(label)
        
        visualEffectView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        visualEffectView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        visualEffectView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        visualEffectView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        
        label.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
        label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
        label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8).isActive = true
        label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        guard let visualEffectView else {
            return
        }
        visualEffectView.layer.cornerRadius = visualEffectView.frame.height / 2
        layer.cornerRadius = frame.height / 2
    }
    
    func set(text: String) {
        guard let label else {
            return
        }
        
        label.text = text
    }
}