IOS自定义UIButton 解决按钮存在下滑线的问题

UIButton在苹果6plus上有下滑线

解决方案:
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
    }
    

}

你可能感兴趣的:(ios)