Swift UI之UILabel 和 UIButton(五)

前端之家收集整理的这篇文章主要介绍了Swift UI之UILabel 和 UIButton(五)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swift_Demo https://github.com/Zhangjingwang1993/Swift.git

UILabel

func createSubviewsOfLabel()
    {
        // 创建Label,之后设置frame
        let label = UILabel()
        label.frame = CGRectMake(10,10,150,20)
        self.view.addSubview(label)


        let label1 = UILabel(frame: CGRectMake(10,50,20))
        self.view.addSubview(label1)

        // 属性的设置跟OC基本类似
        label.text = "This is a Label";
        label.textColor = UIColor.yellowColor()
        label.font = UIFont.systemFontOfSize(14)

        label1.backgroundColor = UIColor.clearColor()

        // 创建富文本
        let attrString = NSMutableAttributedString(string: "This is a attrString")

        // 设置字体的大小
        attrString.addAttribute(NSFontAttributeName,value: UIFont.systemFontOfSize(20),range: NSMakeRange(2,5))

        // 设置字体的颜色
        attrString.addAttribute(NSForegroundColorAttributeName,value: UIColor.redColor(),5))

        // 这只下划线
        attrString.addAttribute(NSUnderlineStyleAttributeName,value: NSUnderlineStyle.StyleSingle.rawValue,5))

        label1.attributedText = attrString
    }

UIButton

func createSubviewsOfButton()
    {
        // 创建
        let button = UIButton.init(type: UIButtonType.Custom)
        // 还可以这样 let button = UIButton(type: UIButtonType.Custom)
        button.frame = CGRectMake(10,80,50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("iambutton",forState: UIControlState.Normal)
        button.addTarget(self,action: Selector("click:"),forControlEvents: UIControlEvents.TouchUpInside)
        button.layer.cornerRadius = 5
        self.view.addSubview(button)

    }

点击方法

func click(button:UIButton)
    {
        print("点击")
    }
原文链接:https://www.f2er.com/swift/324298.html

猜你在找的Swift相关文章