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

        

    }

 

posted @ 2016-11-04 17:56  执笔葬青春  阅读(2273)  评论(0编辑  收藏  举报