• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift循环遍历集合方法发布时间:2022-07-13
下一篇:
swift4.0Http请求发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap