swift學習之label,button,imageView,字串,數組,字典

來源:互聯網
上載者:User

標籤:字元   enabled   turn   editing   resign   tle   let   targe   make   

import UIKit

 

class ViewController: UIViewController,UITextFieldDelegate {

    

    var textField: UITextField!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //背景顏色

        self.view.backgroundColor = UIColor.whiteColor()

        

      //label

        let lable = UILabel(frame:CGRect(x: 10, y: 30, width: 200, height: 70))

        lable.backgroundColor = UIColor.blueColor()

        

        lable.font = UIFont(name: "HelveticaNeue-Bold", size:18)

        self.view.addSubview(lable)

        

        lable.textAlignment = NSTextAlignment.Center

        lable.textColor = UIColor.purpleColor()

        

        lable.shadowColor = UIColor.lightGrayColor()

        lable.shadowOffset = CGSize(width: -5, height: 5)

        

        lable.lineBreakMode = NSLineBreakMode.ByCharWrapping

        lable.adjustsFontSizeToFitWidth = true

        

        lable.highlighted = true

        lable.highlightedTextColor = UIColor.redColor()

        

        let mutableStr = NSMutableAttributedString(string: "我是中國人我驕傲")

        mutableStr.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 38)!, range: NSMakeRange(0,4))

        

        mutableStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(),

            range: NSMakeRange(0, 4))

        //設定文字背景顏色

        mutableStr.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),

            range: NSMakeRange(3,3))

        lable.attributedText = mutableStr

        

        //button

//        let button = UIButton(frame:CGRectMake(10, 150, 100, 30))

        let button:UIButton = UIButton(type:.ContactAdd)

        //設定按鈕位置和大小

        button.frame = CGRectMake(10, 150, 100, 30)

        //設定按鈕文字

        button.setTitle("按鈕", forState:UIControlState.Normal)

        self.view.addSubview(button);

        

        button.setTitle("普通狀態", forState:UIControlState.Normal) //普通狀態下的文字

        

        button.setTitleColor(UIColor.redColor(),forState: .Normal) //普通狀態下文字的顏色

    

        button.setTitleShadowColor(UIColor.greenColor(),forState:.Normal) //普通狀態下文字陰影的顏色

        

        button.backgroundColor=UIColor.grayColor()

        

        button.setImage(UIImage(named:"icon1"),forState:.Normal)  //設定表徵圖

        button.adjustsImageWhenHighlighted=false //使觸摸模式下按鈕也不會變暗

        button.adjustsImageWhenDisabled=false //使禁用模式下按鈕也不會變暗

        

        button.setBackgroundImage(UIImage(named:"background1"),forState:.Normal)

        

         //不傳遞觸摸對象(即點擊的按鈕)

        button.addTarget(self,action:#selector(ViewController.tapped),forControlEvents:.TouchUpInside)

        //    button.addTarget(self,action:#selector(tapped(_:)),forControlEvents:.TouchUpInside)

        

        

        // UITextField

         textField = UITextField(frame: CGRect(x: 30, y: 190, width: 160, height: 30))

        textField.borderStyle = UITextBorderStyle.RoundedRect

        self.view.addSubview(textField)

        textField.delegate = self

        textField.placeholder = "請輸入使用者名稱"

    

        textField.adjustsFontSizeToFitWidth = true //當文字超出文字框寬度時,自動調整文字大小

        textField.minimumFontSize = 14 //最小可縮小的字型大小

        

        textField.textAlignment = .Left //水平靠左對齊

        

        textField.contentVerticalAlignment = .Center  //垂直置中對齊

        

        //背景圖片設定

//        textField.borderStyle = .None //先要去除邊框樣式

//        textField.background=UIImage(named:"background1");

        

        //清除按鈕(輸入框內右側小叉) 

        textField.clearButtonMode=UITextFieldViewMode.WhileEditing  //編輯時出現清除按鈕

        

        // UIImageView的使用

        let imageView = UIImageView(image: UIImage(named: "icon"))

        imageView.frame = CGRectMake(30, 230, 100, 100)

        self.view.addSubview(imageView)

        

        let tap111 = UITapGestureRecognizer(target: self, action:#selector(ViewController.tap))

        

       imageView.userInteractionEnabled = true

        imageView.addGestureRecognizer(tap111)

        

        //字串使用

        

        //1 判斷字串是否為空白

        

        let test1Str="111"

        let test1Str2:String = String("ssss");

        

        print("test1Str" + (test1Str.isEmpty ? "沒有值" : "有值"))

        

        print("test1Str2" + (test1Str2.isEmpty ? "沒有值" : "有值"))

        print("88737979879879")

        

        

         //數組的使用

        let array = ["1","2","3"]

        for item in array{

            print("------" + item);

        }

        //字典的使用

        let dict = ["1":"111","2":"222","3":"333"]

        let arr:NSMutableArray=NSMutableArray.init(capacity: 100);

        for key in dict.values{

            print(key)

            arr.addObject(key)

        }

        print(arr)

    }

    

    func tap(){

      print("++++++++++++11111111")

    }

    

//    傳遞觸摸對象(即點擊的按鈕),需要在定義action參數時,方法名稱後面帶上冒號

    func tapped(button:UIButton){

        print(button.titleForState(.Normal))

    }

    

    

    //收合鍵盤

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

        textField.resignFirstResponder()

    }

    

    func textFieldShouldReturn(textField: UITextField) -> Bool

    {

        textField.resignFirstResponder()

        return true;

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        

    }

}

 

swift學習之label,button,imageView,字串,數組,字典

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.