美文网首页iOS Development
swift --- 标签简单布局

swift --- 标签简单布局

作者: 71150ce14a00 | 来源:发表于2017-04-14 15:34 被阅读21次

    var x:CGFloat = 15
    var y:CGFloat = 15
    for indext in 0..<tagName.count {
    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 --- 标签简单布局

      本文链接:https://www.haomeiwen.com/subject/cdnwattx.html