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. Angular(二)

    Angular开发者指南(二)概念概述   template(模板):带有附加标记的模板HTMLdirectives(指令):使用自定义属性和元素扩展HTMLmodel(模型):用户在视图中显示的数据 ...

  2. JQ和JS的等价代码

    JQ与JS等价代码   选择器 //jquery var els = $(".el"); //原生方法 var els = document.querySelectorAll(&q ...

  3. Leetcode 5039. 移动石子直到连续

    第134次周赛 5039. 移动石子直到连续 5039. 移动石子直到连续 三枚石子放置在数轴上,位置分别为 a,b,c. 每一回合,我们假设这三枚石子当前分别位于位置 x, y, z 且 x < ...

  4. Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

    解决办法: 两个方案:1.注册处理函数时,用如下方式,明确声明为不是被动的window.addEventListener('touchmove', func, { passive: false }) ...

  5. 四剑客(sed)

    一. sed sed简介: 用Linux环境中的编辑器程序来编辑文本文件.这些编辑器可以让你用简单命令或鼠标单击来轻松地处理文本文件中的文本.但有时候,你会发现需要自动处理文本文件,可你又不想动用全副 ...

  6. 16)用了session会话技术

    为什么用session会话技术? 因为假如你进入后台,不可能随意进入,即使你的验证通过了,那么还需要一个变量来存一个标志,假如标志的值是yes,那么我们可以直接进入后台的首页,无需验证,但是,标志是n ...

  7. pycharm中无法调用pip的安装包

    https://blog.csdn.net/sinat_23619409/article/details/79962518 较详细:https://blog.csdn.net/weixin_41287 ...

  8. spring的事务,详解@Transactional

    事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持. Spring 事务管理分为编程式和声明式的两种方式. 编程式事务指的是通过编码方式实现事务,编程式事务管理使用 ...

  9. 3dmax2020卸载/安装失败/如何彻底卸载清除干净3dmax2020注册表和文件的方法

    3dmax2020提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dmax2020失败提示3dmax2020安装未完成,某些产品无法安装,也有时候想重新安装3 ...

  10. stress施压案例分析——cpu、io、mem【命令分析】

    stress施压命令分析 一.stress --cpu 1 --timeout 600  分析现象?负载为啥这么高?top命令查看用户进程消耗的cpu过高(stress进程消耗的) 分析现象,可以看出 ...