加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift UI之UILabel 和 UIButton(五)

发布时间:2020-12-14 07:09:51 所属栏目:百科 来源:网络整理
导读: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 = U

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("点击")
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读