iOS Swift 开发语言之初接触,纯代码创建UIView,UITableView,UICollectionView

1. 初始化Label设置AttributeString

override func viewDidLoad() {
let label = UILabel(frame:CGRect(x:20,y:80,width:(self.view.frame.size.width - 40),height:30))
        label.font = UIFont.systemFont(ofSize: 15)
        label.backgroundColor = UIColor.lightGray
        label.textColor = UIColor.orange
        label.isHighlighted = true
        label.highlightedTextColor = UIColor.red
        label.textAlignment = NSTextAlignment.center
        self.view.addSubview(label)
     let dict = [NSForegroundColorAttributeName:UIColor.purple,NSFontAttributeName:UIFont.boldSystemFont(ofSize: 15)]
        let labelStr = "hello Swift,Welcome to Swift"
        let nameStr = NSMutableAttributedString.init(string: labelStr, attributes: dict)
        nameStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange(location:12,length:7))
        nameStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 18, weight: 1.5), range: NSRange(location:12,length:7))
        nameStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSRange(location:23,length:5))
        label.attributedText = nameStr
}

 


2. 初始化Button,点击button弹出对话框

var isShow = false
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // button
        let button = UIButton(frame:CGRect(x:100,y:140,width:120,height:40))
        button.setTitle("Demo", for: UIControlState.normal)
        button.setTitleColor(UIColor.blue, for: UIControlState.normal)
        button.backgroundColor = UIColor.orange
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.addTarget(self, action: #selector(buttonClick(_:)), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
    }
func buttonClick(_ button :UIButton) {
        // Dispose of any resources that can be recreated.
        if isShow {
            isShow = false
        }
        else
        {
            let alertView = UIAlertController.init(title: "弹出框", message: "开始Swift的旅程吧", preferredStyle: UIAlertControllerStyle.alert)
            let action = UIAlertAction.init(title: "关闭", style: UIAlertActionStyle.default, handler: { (action) in
                print("点击关闭弹按钮")
            })
            let action1 = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.default, handler: { (action) in
                print("点击确定按钮")
            })
            alertView.addAction(action)
            alertView.addAction(action1)
            self.present(alertView, animated: true, completion: { 
                
            })
            isShow = true
        }
    }

3.初始化SearchBar

class SearchBarViewController: UIViewController,UISearchBarDelegate {
    var searchBar:UISearchBar!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = false
        // Do any additional setup after loading the view.
        searchBar = UISearchBar.init(frame: CGRect(x:10,y:74,width:(self.view.frame.size.width - 20),height:40))
        searchBar.delegate = self;
        searchBar.placeholder = "请搜索热门商品吧"
        searchBar.searchBarStyle = UISearchBarStyle.minimal
        searchBar.setShowsCancelButton(true, animated: true)
        searchBar.tintColor = UIColor.orange
        searchBar.showsScopeBar = true
//        searchBar.showsSearchResultsButton = true
        searchBar.showsBookmarkButton = true
//        searchBar.prompt = "QQ"
        searchBar.barTintColor = UIColor.blue
        self.view.addSubview(searchBar)
    }
    
    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        return true
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        searchBar.text = nil
        print("点击cancle按钮")
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        print("点击键盘搜索")
    }
    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        print("结束编辑\(searchBar.text)")
    }
    
    func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let boolStr = searchBar.text as NSString!
        print("****正在输入的信息\(text) 之前的String:\(boolStr)")
        return true
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        print("正在输入的信息\(searchBar.text)")
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        print("开始编辑\(searchBar.text)")
    }
    
    func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
        print("点击书签按钮")
    }
}

> 跟多swift代码请到https://github.com/chenhuc/SwiftDemo.git

posted @ 2016-11-23 16:55  ChenHuChang  阅读(3008)  评论(0编辑  收藏  举报