IOSカスタムUIButtonボタンのスライドラインの問題を解決

2216 ワード

UIButtonはアップル6 plusに下り線がある
ソリューション:1.button上の文字を設定する場合は、次の方法を使用します.
/**
             
     */
    func setButViewTitle(content:String){
        let attributedString = NSMutableAttributedString(string: content)

        attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.styleNone.rawValue, range: NSRange(location: 0, length: attributedString.length))

        self.setAttributedTitle(attributedString, for: .normal)
    }

2.カスタムUIButton
//
//  MyUIButtonView.swift
//  wulian
//
//  Created by              on 2019/3/5.
//  Copyright © 2019             . All rights reserved.
//         

import UIKit

class MyUIButtonView: UIButton {
    private var butLabel:UILabel?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initView()
    }
    
    private func initView(){
        self.backgroundColor = UIColor(hexString: "#FFDE02")
        //      
        //self.gradientColor(["#FFD200","#FFDE02"])
        self.viewSettingRadius(5)//    
        self.setTitleColor(UIColor.clear, for: .normal)
        self.setTitleColor(UIColor.clear, for: .selected)
        self.setTitleColor(UIColor.clear, for: .disabled)
        self.setTitleColor(UIColor.clear, for: .highlighted)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 0)
        //setButViewTitle(content:(self.titleLabel?.text)!)
        
        butLabel = UILabel()
        butLabel?.font = UIFont.systemFont(ofSize: 15)
        butLabel?.textColor = UIColor.black
        butLabel?.text = self.titleLabel?.text
        self.addSubview(butLabel!)
        butLabel?.snp.makeConstraints({ (make) in
            make.center.equalToSuperview()
        })
    }
    
    override func setTitle(_ title: String?, for state: UIControlState) {
        super.setTitle(title, for: state)
        butLabel?.text = title
    }
    
    override func setTitleColor(_ color: UIColor?, for state: UIControlState) {
        super.setTitleColor(color, for: state)
        butLabel?.textColor = color
    }
    

}