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