swift --- 标签简单布局

var x:CGFloat = 15
var y:CGFloat = 15
for indext in 0.. let str = tagName[indext]
let size = str.getTexWidth(textStr: str, font: UIFont.systemFont(ofSize: 15), height: 20)
let sizeW = size.width + 30
if x + sizeW > screenW - 20 {
x = 15
y = y + size.height + 30
}
let tagBtn = UIButton.init(frame: .init(x: x, y: y, width: sizeW, height: 30))
tagBtn.layer.cornerRadius = 15
tagBtn.layer.masksToBounds = true
tagBtn.setTitle(str, for: .normal)
tagBtn.setTitleColor(UIColor.lightGray, for: .normal)
tagBtn.layer.borderWidth = 1
tagBtn.layer.borderColor = UIColor.lightGray.cgColor
tagBtn.tag = indext + 100
tagBtn.addTarget(self, action: #selector(tagBtnAction(btn:)), for: .touchUpInside)

        self.view.addSubview(tagBtn)
        
        x = x + sizeW + 10
    }
swift --- 标签简单布局_第1张图片
效果图

你可能感兴趣的:(swift --- 标签简单布局)