snpKit 关于自动布局

//
//  TRRateHeardView.swift
//  TREvaluation
//
//  Created by Mr.H on 2017/4/25.
//  Copyright © 2017年 long. All rights reserved.
//

import UIKit

class TRRateHeardView: TSView {
    var array = [TRRateHeardModel]() //需要创建的个数

    init(array:[TRRateHeardModel]) {
        self.array = array
        super.init()
    }

    override func viewDidLoad() { self.creatView(array: array) }

    func creatView(array:[TRRateHeardModel]) {
        var tempLable:UIView? = nil
        let multipliedBy:Float = 1 / Float(array.count)
        array.forEach {
            let view = self.creatlabel(number: array.count, model:$0)
            self.addSubview(view)
            if tempLable == nil {
                view.snp.makeConstraints {
                    $0.left.equalToSuperview()
                    $0.centerY.equalToSuperview()
                    $0.width.equalToSuperview().multipliedBy(multipliedBy)
                }
            }else {
                view.snp.makeConstraints {
                    $0.left.equalTo((tempLable?.snp.right)!)
                    $0.centerY.equalToSuperview()
                    $0.width.equalToSuperview().multipliedBy(multipliedBy)
                }
            }
            tempLable = view
        }
    }

    func creatlabel(number: Int,model: TRRateHeardModel) -> UIView {
        let bottomLabel = UIView()
        let dotLable = UILabel()
        let textLable = UILabel()
        bottomLabel.addSubview(dotLable)
        bottomLabel.addSubview(textLable)
        let dotwidth:CGFloat = model.dotwidth
        let spacing:CGFloat = model.spacing
        let centeX = spacing / 2 + dotwidth/2
        dotLable.layer.cornerRadius = dotwidth/2
        dotLable.layer.masksToBounds = true
        dotLable.backgroundColor = model.dotcolor

        textLable.text = model.title
        textLable.font = model.textFont
        textLable.textColor = model.textColor
        textLable.adjustsFontSizeToFitWidth = true

        dotLable.snp.makeConstraints {
            $0.right.equalTo(textLable.snp.left).offset(spacing)
            $0.centerY.equalToSuperview()
            $0.size.equalTo(CGSize(width: dotwidth, height: dotwidth))
        }
        textLable.snp.makeConstraints {
            $0.centerY.equalToSuperview()
            $0.height.equalToSuperview()
            $0.centerX.equalToSuperview().offset(centeX)
        }
        return bottomLabel
    }

    convenience required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

struct TRRateHeardModel {
    var title:    String   //要显示的文字
    var textColor:UIColor  //文字颜色
    var dotcolor: UIColor  //圆点颜色
    var dotwidth: CGFloat  //圆点大小
    var spacing:  CGFloat  //红点和文字间距
    var textFont: UIFont   //文字大小

    init(title:    String,
         textColor:UIColor = UIColor.gray ,
         dotcolor: UIColor = UIColor.red,
         dotwidth: CGFloat = 12,
         spacing:  CGFloat = -6,
         textFont: UIFont  = UIFont.systemFont(ofSize: 16)) {
        self.title     = title
        self.textColor = textColor
        self.dotcolor  = dotcolor
        self.dotwidth  = dotwidth
        self.spacing   = spacing
        self.textFont  = textFont

    }

}

调用

 let array = [TRRateHeardModel(title: "王木木"),
                     TRRateHeardModel(title: "王木木"),
                     TRRateHeardModel(title: "王木木")]

        self.heardView = TRRateHeardView(array: array)

        self.view.addSubview(heardView)

        self.heardView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值