1. 初始化Label设置AttributeString

override func viewDidLoad() {
let label = UILabel(frame:CGRect(x:,y:,width:(self.view.frame.size.width - ),height:))
        label.font = UIFont.systemFont(ofSize: )
        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: )]
        let labelStr = "hello Swift,Welcome to Swift"
        let nameStr = NSMutableAttributedString.init(string: labelStr, attributes: dict)
        nameStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange(location:,length:))
        nameStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: , weight: ,length:))
        nameStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSRange(location:,length:))
        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:,y:,width:,height:))
        button.setTitle("Demo", for: UIControlState.normal)
        button.setTitleColor(UIColor.blue, for: UIControlState.normal)
        button.backgroundColor = UIColor.orange
        button.titleLabel?.font = UIFont.systemFont(ofSize: )
        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:,y:,width:(self.view.frame.size.width - ),height:))
        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

iOS Swift 开发语言之初接触,纯代码创建UIView,UITableView,UICollectionView的更多相关文章

  1. iOS开发 纯代码创建UICollectionView

    转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xi ...

  2. iOS高仿app源码:纯代码打造高仿优质《内涵段子》

    iOS高仿app源码:纯代码打造高仿优质<内涵段子>收藏下来 字数1950 阅读4999 评论173 喜欢133 Github 地址 https://github.com/Charlesy ...

  3. iOS回顾笔记( 01 )-- XIB和纯代码创建应用的对比

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  4. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  5. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

  6. swift 之 纯代码创建 cell

    初学swift 但是网上只有很多swift用xib创建的cell,就算是有也不是我想要的.今天自己弄了一个不用xib纯代码写的,来上代码 博客地址: https://github.com/liguol ...

  7. 【Android】纯代码创建页面布局(含异步加载图片)

    开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1 先看看总体效果 本示例是基于Fragment进行的,直接上代码: [界面结构] 在 F ...

  8. ios新开发语言swift 新手教程

    http://gashero.iteye.com/blog/2075324 视频教程:http://edu.51cto.com/lesson/id-26464.html

  9. iOS Swift开发的一些坑

    0.人难招,特别是对于没钱的小团队,大多数的iOS开发者没有Swift经验,有经验的也并不是很深入 0.1.语言unwrap坑,虽然有自动修正提示,但感觉代码过程不流畅. 1.Realm的缺憾: 最近 ...

随机推荐

  1. VSFTP服务搭建

    title date tags layout CentOS6.5 Vsftp服务安装与配置 2018-09-04 Centos6.5服务器搭建 post 1.安装vsftp服务 [root@local ...

  2. servletHomeWork

    2. http全称是什么? 超文本传输协议(HTTP, HyperText Transfer Protocol)是互联网上应用为最广泛的一种网络协议. 3.http协议是无状态的协议是什么意思?请说明 ...

  3. 场景实践篇二:Nginx作为缓存

    cd /etc/nginx/conf.d/ vim cache_test.conf

  4. c# winForm 将窗体状态栏StatusStrip 分成左中右三部分 右边显示当前时间

    实现效果:通过StatusStrip显示窗体状态栏同时将状态栏分成三部分居左边显示相关文字信息中间空白显示居右边显示时间信息 1.创建窗体及添加StatusStrip  默认StatusStrip名称 ...

  5. 邪恶的csrf

    关于csrf是啥我就不多说了 进入正文 场景模拟 场景一 在一个bbs社区里,用户在发言的时候会发出一个这样的GET请求: #!html GET /talk.php?msg=hello HTTP/1. ...

  6. 系统学习Javaweb9----BootStrap1

    学习内容: 1.BootStrap的简述 2.BootStrap环境搭建 3.BootStrap环境搭建-基本模板创建 4.BootStrap环境搭建-基本模板讲解 5.BootStrap布局容器 6 ...

  7. js使用心得——避免全局变量冲突的小技巧

    在写js代码的时候,经常会因为这样或者那样的原因用到全局变量,如果全局变量只在一个js里使用,那就没问题,但如果变量在不同的js文件里出现,这时隐藏的问题就会开始暴露,也许你能很快修复出现的BUG,又 ...

  8. 吴裕雄--天生自然python学习笔记:pandas模块DataFrame 数据的修改及排序

    import pandas as pd datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]] ...

  9. 手撸GBDT原理(未完成)

    一直对GBDT里面的具体计算逻辑不太清楚,在网上发现了一篇好博客. 先上总结的关系图 GBDT对类别变量是怎么处理的? 这些东西都是在网上发现的,讲的挺好的. GBDT原理与Sklearn源码分析-回 ...

  10. Telnet和SSH区别

    首先,telnet和ssh都是连接远程计算机的连接协议,可以完成对完成计算机的控制,方便维护.其次,他们都是基于TCP/IP协议下的,所以连接时都需要知道目标机的网址或者域名,第三,他们都是与远程主机 ...