• AutoLayout与UIbutton


    1、UIButton贴近右边,高度固定,宽度跟随标题变化

    class TestButton: UIButton {
    }
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let btn = TestButton(type: .custom)
            btn.setTitle("TestButton 1 2 3 sdasdad dadasd", for: .normal)
            btn.setTitleColor(.black, for: .normal)
            btn.layer.borderColor = UIColor.red.cgColor
            btn.layer.borderWidth = 1
            view.addSubview(btn)
    
            btn.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview().offset(-20)
                make.height.equalTo(30)
            }
        }
    }
    

      效果:

    2、限制宽度,多了就换行

    class TestButton: UIButton {
    }
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let btn = TestButton(type: .custom)
            btn.setTitle("TestButton 1 2 3 sdasdad dadasd,TestButton 1 2 3 sdasdad dadasd,TestButton 1 2 3 sdasdad dadasd", for: .normal)
            btn.setTitleColor(.black, for: .normal)
            btn.titleLabel?.numberOfLines = 0
            btn.layer.borderColor = UIColor.red.cgColor
            btn.layer.borderWidth = 1
            view.addSubview(btn)
    
            btn.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview().offset(-20)
    //            make.height.equalTo(30)
                make.width.lessThanOrEqualTo(200)
            }
        }
    }
    

      效果:

     明显,UIButton的框太小了,限制了titleLabel,现在想实现,button的高度和文字高度一样

    修改如下

    class TestButton: UIButton {
        override var intrinsicContentSize: CGSize {
            return titleLabel!.sizeThatFits(CGSize( titleLabel!.preferredMaxLayoutWidth, height: .greatestFiniteMagnitude))
        }
        override func layoutSubviews() {
            titleLabel?.preferredMaxLayoutWidth = frame.size.width
            super.layoutSubviews()
        }
    }
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let btn = TestButton(type: .custom)
            btn.setTitle("TestButton 1 2 3 sdasdad dadasd,TestButton 1 2 3 sdasdad dadasd,TestButton 1 2 3 sdasdad dadasd", for: .normal)
            btn.setTitleColor(.black, for: .normal)
            btn.titleLabel?.numberOfLines = 0
            btn.layer.borderColor = UIColor.red.cgColor
            btn.layer.borderWidth = 1
            view.addSubview(btn)
    
            btn.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview().offset(-20)
    //            make.height.equalTo(30)
                make.width.lessThanOrEqualTo(200)
            }
        }
    }
    

      效果:

    总结:

    Autolayout和intrinsicContentSize有关,UIButton的intrinsicContentSize默认和标题长短没有关系,和UILabel不一样

  • 相关阅读:
    关于IE6不能兼容LCUC使用的PNG透明图象
    rmvb压制中高级技巧
    不错的课件网站
    C#程序多用户只启动一个进程的方法
    不可想像的加密光盘复制工具
    检测远程URL是否存在的三种方法
    请哪里有英文单词单复数转换的代码?
    一些感想,欢迎拍砖
    Some thoughts on my own O/R Mapping or Code Generation tools
    有了net send,谁还用IM?
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/11815787.html
Copyright © 2020-2023  润新知