Swift3.0學習之Button


1.根據字符串的長度確定Button的長度(button的高度給定)

 let hight:CGFloat = 30.0

 let size1 = CGSize(width: getLabWidth(labelStr: "我的升級換代卡號多少", font: UIFont.systemFont(ofSize: 14), height: hight), height: hight)

 

        let rect1 = CGRect(origin: CGPoint(x:40,y:200), size: size1)//設置Button的frame

 let bt1:UIButton = UIButton.init(frame: rect1)

        bt1.backgroundColor = UIColor.red//設置Button的背景色

        bt1.titleLabel?.font = UIFont.systemFont(ofSize: 12)//設置Button的字體大小

        bt1.titleLabel?.numberOfLines = 0;//設置Button的文本換行

        

        bt1.setTitle("我的手機點哈卡的卡刪繁就簡哈哈放假薩法介紹", for: UIControlState.normal)//設置Button的文本內容

        bt1.addTarget(self, action: #selector(didBt), for: UIControlEvents.touchUpInside)//設置Button的點擊事件

        bt1.isSelected = true//設置Button的選中狀態

        bt1.layer.borderWidth = 2.0;//設置Button的邊框寬度

        bt1.layer.borderColor = UIColor.blue.cgColor//設置Button的邊框顏色

        bt1.layer.cornerRadius = 10;//設置Button的圓角弧度

        bt1.layer.masksToBounds = true

        self.view.addSubview(bt1)//把button添加到控制器里

 2.根據字符串的長度確定Button的高度(button的寬度給定)

        let width:CGFloat = 60.0;

        let size = CGSize(width: width, height: getLabHeigh(labelStr: "我的升級換代卡號多少", font: UIFont.systemFont(ofSize: 14), width: width))

        let rect = CGRect(origin: CGPoint(x:20,y:100), size: size)//設置Button的frame

        let bt:UIButton = UIButton.init(frame: rect)

        bt.backgroundColor = UIColor.red//設置Button的背景色

        bt.titleLabel?.font = UIFont.systemFont(ofSize: 12)//設置Button的文本字體大小

        bt.titleLabel?.numberOfLines = 0;

        bt.setTitle("我的升級換代卡號多少", for: UIControlState.normal)//設置Button的文本內容

        bt.addTarget(self, action: #selector(didBt), for: UIControlEvents.touchUpInside)//設置Button的點擊事件

        bt.isSelected = true//設置Button的選中狀態

        self.view.addSubview(bt)

3.相關方法

 @objc func didBt(bt:UIButton){

        if bt.isSelected == true {

            self.view.backgroundColor = UIColor.red

            bt.isSelected = false

        }else{

      self.view.backgroundColor = UIColor.yellow

            bt.isSelected = true

        }

        

    }

    func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat {

        

        let statusLabelText: NSString = labelStr as NSString

        let size = CGSize(width: width, height: 900)

       

        let dic = NSDictionary(object: font, forKey: NSFontAttributeName as NSCopying)

        

        let strSize = statusLabelText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context: nil).size

        

        return strSize.height

        

    }

    

    

    

    func getLabWidth(labelStr:String,font:UIFont,height:CGFloat) -> CGFloat {

        

        let statusLabelText: NSString = labelStr as NSString

        

        let size = CGSize(width:900,height: height)

        

        let dic = NSDictionary(object: font, forKey: NSFontAttributeName as NSCopying)

        

        let strSize = statusLabelText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context: nil).size

        

        return strSize.width

        

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM