hussc
5/20/2019 - 2:25 AM

SCMainButton.swift

//
//  SCMainButton.swift
//  Not in a project :)
//
//  Created by Hussein AlRyalat on 5/20/19.
//  Copyright © 2019 SketchMe. All rights reserved.
//

import UIKit

@IBDesignable class SCMainButton: UIButton {
    
    fileprivate lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = tintColor
        return view
    }()
    
    
    // in case you need to change it for later
    var lineHeight: CGFloat = 2 {
        didSet {
            setNeedsLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    fileprivate func setup(){
        
        // add additional space
        contentEdgeInsets = UIEdgeInsets(top: 3, left: 8, bottom: 8, right: 8)
        
        // customize the font
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        setTitleColor(.black, for: .normal)
        
        
        addSubview(lineView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        lineView.frame = CGRect(x: contentEdgeInsets.left, y: bounds.height - lineHeight, width: bounds.width - (contentEdgeInsets.right + contentEdgeInsets.left), height: lineHeight)
        lineView.frame.size.height = lineHeight
    }
    
    override func tintColorDidChange() {
        super.tintColorDidChange()
        lineView.backgroundColor = tintColor
    }
}