Ios swift 3:创建材料设计芯片

Ios swift 3:创建材料设计芯片,ios,swift,material-design,cosmicmind,Ios,Swift,Material Design,Cosmicmind,嘿,嘿 几天以来,我一直试图制作“材料设计芯片”,但只成功了一半 我所做的最成功的尝试是从“Button”创建一个子类(Button是UIButton的子类,UIButton是在他的swift MaterialDesign框架中创建的) 对于那些不知道我的意思是“筹码”的人: 我举的例子: 简单/不可删除芯片 要创建此按钮,请执行以下操作: let button = ChipButton() button.title = "default chip" view.layout(b

嘿,嘿

几天以来,我一直试图制作“材料设计芯片”,但只成功了一半

我所做的最成功的尝试是从“Button”创建一个子类(Button是UIButton的子类,UIButton是在他的swift MaterialDesign框架中创建的)

对于那些不知道我的意思是“筹码”的人:

我举的例子:

简单/不可删除芯片 要创建此按钮,请执行以下操作:

let button = ChipButton()
    button.title = "default chip"

    view.layout(button).height(32).center(offsetY: -150)
触点芯片/图标芯片 我想在这里

  • 将UIImageView调整为芯片高度(32点)

  • 将UIImage的大小缩小一点(到20点)

  • 更改UIImage的颜色的步骤

    I tryed with self.imageView?.tintColor = Color.black but nothing changed
    
  • 可删除芯片 在这里,我尝试使用imageEdgeInsets和titleEdgeInsets切换标签的位置,但self.frame.size.width返回的宽度不正确(可能是自动布局的原因,但我不确定)

    帮助 希望有人能帮助我

    另外,我是swift/xcode的新手,它是一种简单的/不可删除的芯片 这里没有什么变化。 看看这个问题

    触点芯片/图标芯片 创造物剪掉了:

    open func prepareChipIconButton () {
        let icon: UIImage? = #imageLiteral(resourceName: "ic_close_white_24px")
        let button = ChipIconButton(image: icon, title: "icon chip")
        view.layout(button).height(32).center(offsetY: 0)
    }
    
    open func prepareChipDeleteableButton () {
        let button = ChipDeleteableButton()
        button.title    = "deleteable chip"
        button.swapLabelWithImage()
    
        view.layout(button).height(32).center(offsetY: 150)
    }
    
    可删除芯片 创造物剪掉了:

    open func prepareChipIconButton () {
        let icon: UIImage? = #imageLiteral(resourceName: "ic_close_white_24px")
        let button = ChipIconButton(image: icon, title: "icon chip")
        view.layout(button).height(32).center(offsetY: 0)
    }
    
    open func prepareChipDeleteableButton () {
        let button = ChipDeleteableButton()
        button.title    = "deleteable chip"
        button.swapLabelWithImage()
    
        view.layout(button).height(32).center(offsetY: 150)
    }
    
    更多信息:
    • 在我的ViewController中调用的创建函数r,在ViewWillAspect()中
    • 为什么我的可删除芯片有一个额外的功能因为我让AutoLayout完成他的工作,然后我可以通过let
      rightLableSize=self.titleLabel?.sizeThatFits((self.titleLabel?.frame.size)!)获得标签的必要“字符串宽度”

    我找到了解决方法:>3。要更改任何按钮类型(系统按钮除外)的UIImage的颜色,必须将.withRenderingMode(.alwaysTemplate)添加到img。例如:self.image=UIImage(名为:“image name”)。withRenderingMode(.alwaysTemplate)UIImage(名为:“image name”)?。withRenderingMode(.alwaysTemplate)问号很重要
    import UIKit
    import Material
    
    class ChipIconButton: ChipButton {
        public convenience init(image: UIImage?, title: String?){
            self.init()
            prepare(with: image, title: title)
        }
    
        private func prepare(with image: UIImage?, title: String?) {
            //self.imageView?.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
            let myThumb = image?.resize(toWidth: 20)?.resize(toHeight: 20)
            let shapeView = UIView(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
            shapeView.backgroundColor = UIColor.darkGray
            shapeView.cornerRadiusPreset = .cornerRadius5
            shapeView.zPosition = (self.imageView?.zPosition)! - 1
    
            self.addSubview(shapeView)
    
    
            self.image = myThumb?.withRenderingMode(.alwaysTemplate)
            self.title = title
            self.imageView?.tintColor  = self.backgroundColor
            self.imageEdgeInsets    = EdgeInsets(top: 0, left: -28, bottom: 0, right: 0)
            self.titleEdgeInsets    = EdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            self.contentEdgeInsets  = EdgeInsets(top: 0, left: 20, bottom: 0, right: 12)
        }
    }
    
    open func prepareChipIconButton () {
        let icon: UIImage? = #imageLiteral(resourceName: "ic_close_white_24px")
        let button = ChipIconButton(image: icon, title: "icon chip")
        view.layout(button).height(32).center(offsetY: 0)
    }
    
    import UIKit
    import Material
    
    class ChipDeleteableButton: ChipButton {
    
        override func prepare() {
            super.prepare()
    
            let img = #imageLiteral(resourceName: "ic_close_white_24px").resize(toHeight:18)?.resize(toWidth: 18)
            let myThumb = img?.withRenderingMode(.alwaysTemplate)
    
            self.image = myThumb
            self.title = title
    
            self.imageView?.tintColor  = self.backgroundColor
            self.isUserInteractionEnabled = true
    
            self.addTarget(self, action: #selector(clickAction), for: .touchUpInside)
            self.imageView?.isUserInteractionEnabled = false
        }
    
        open func swapLabelWithImage() {
            let rightLableSize = self.titleLabel?.sizeThatFits((self.titleLabel?.frame.size)!)
    
            self.imageEdgeInsets    = EdgeInsets(top: 0, left: (rightLableSize?.width)! - 4, bottom: 0, right: 0)
            self.titleEdgeInsets    = EdgeInsets(top: 0, left: -54, bottom: 0, right: 0)
            self.contentEdgeInsets  = EdgeInsets(top: 0, left: 20, bottom: 0, right: 4)
    
            let shapeView = UIView(frame: CGRect(x: self.imageEdgeInsets.left + 19, y: 6, width: 20, height: 20))
            shapeView.backgroundColor = UIColor.darkGray
            shapeView.cornerRadius = shapeView.frame.size.width/2
            shapeView.zPosition = (self.imageView?.zPosition)! - 1
            shapeView.isUserInteractionEnabled = false
    
    
            self.addSubview(shapeView)
        }
    
        internal func clickAction(sender: ChipDeleteableButton) {
            print("do something")
        }
    }
    
    open func prepareChipDeleteableButton () {
        let button = ChipDeleteableButton()
        button.title    = "deleteable chip"
        button.swapLabelWithImage()
    
        view.layout(button).height(32).center(offsetY: 150)
    }