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

Swift UIButton

发布时间:2020-12-14 02:37:35 所属栏目:百科 来源:网络整理
导读:在Swift中UIButton添加点击事件的最大区别在 selector. 下面直接看代码: //// ViewController.swift// 1.UILabel//// Created by zhuming on 16/1/24.// Copyright ? 2016年 zhuming. All rights reserved.//import UIKitclass ViewController: UIViewContro

在Swift中UIButton添加点击事件的最大区别在selector.

下面直接看代码:

//
//  ViewController.swift
//  1.UILabel
//
//  Created by zhuming on 16/1/24.
//  Copyright ? 2016年 zhuming. All rights reserved.
//

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    var coun = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label1.text = "第一个Label标签"

        creatLabel2()
        creatButton()
        // Do any additional setup after loading the view,typically from a nib.
    }
    /**
     创建一个标签对象
     */
    func creatLabel2(){
        // 初始化一个label2对象 并设置frame值
        let label2 = UILabel(frame: CGRect(x: 20,y: 200,width: 200,height: 80))
        label2.text = "第二个Label标签2e3gr35hjym eRGBHRTN HRGBHTN H" // 标签显示内容
        label2.textAlignment = NSTextAlignment.Center //对齐方式
        label2.textColor = UIColor.greenColor()   // 字体颜色
        label2.font = UIFont.systemFontOfSize(12) // 字体大小
        label2.backgroundColor = UIColor.grayColor() // 背景颜色
        label2.shadowColor = UIColor.redColor()  // 阴影颜色
        label2.shadowOffset = CGSizeMake(10,10) // 阴影偏移量
        label2.numberOfLines = 0  // 0:多行显示
        label2.adjustsFontSizeToFitWidth = true  //根据标签长度自动调整字体大小 需要注释掉多行显示才有效果
        // 下面这两个属性 打开字体高亮 和设置字体高亮颜色 会覆盖原来设置的字体颜色
        label2.highlightedTextColor = UIColor.whiteColor()
        label2.highlighted = true
        
        self.view .addSubview(label2)
    }
    func creatButton(){
        let button = UIButton(frame: CGRect(x: 50,y: 300,width: 100,height: 40))
        button.setTitle("按钮",forState: UIControlState.Normal)
        button.backgroundColor = UIColor.redColor()
        button .addTarget(self,action: Selector("btnClick"),forControlEvents: UIControlEvents.TouchUpInside)
        /*
        // OC 里面UIButton添加点击事件
        [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        self.view .addSubview(button)
        */
    }
    func btnClick(){ // 按钮点击事件
        print("按钮按下")
        coun  = coun + 1;
        label1.text = "(coun)"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
效果图:

(编辑:李大同)

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

    推荐文章
      热点阅读