Swift 常用方法总结
发布时间:2020-12-14 06:09:45 所属栏目:百科 来源:网络整理
导读:Swift 常用方法总结 1、设置AttributeStringText //MARK: - 设置富文本 func setAttributeStringText(strFullText:String,andFullTextFont textFont:UIFont,andFullTextColor textColor:UIColor,withChangeText changeText:String,withChangeFont changFont:
Swift 常用方法总结1、设置AttributeStringText//MARK: - 设置富文本 func setAttributeStringText(strFullText:String,andFullTextFont textFont:UIFont,andFullTextColor textColor:UIColor,withChangeText changeText:String,withChangeFont changFont:UIFont,withChangeColor changeColor:UIColor) -> NSAttributedString{ let attributeString = NSMutableAttributedString.init(string: strFullText) //需要改变的文本 var range = NSString.init(string: strFullText).range(of: changeText) attributeString.addAttributes([ NSFontAttributeName:changFont,NSForegroundColorAttributeName:changeColor,],range: range) //不需要改变的文本 let oldText = NSString.init(string: strFullText).replacingOccurrences(of: changeText,with: "") range = NSString.init(string: strFullText).range(of: oldText) attributeString.addAttributes([ NSFontAttributeName:textFont,NSForegroundColorAttributeName:textColor,range: range) return attributeString }
2、是否含有Emoji表情//MARK: - Emoji表情检测 //是否含有Emoji 表情(true 含有) func stringContainsEmoji(string:String)->Bool{ var returnValue = false; let Str = NSString(format: "%@",string); Str.enumerateSubstrings(in: NSMakeRange(0,Str.length),options: NSString.EnumerationOptions.byComposedCharacterSequences) { (substring,substringRange,enclosingRange,stop) in let subStr = NSString(format: "%@",substring!); let hs = subStr.character(at: 0); // surrogate pair if (0xd800 <= hs && hs <= 0xdbff) { if (subStr.length > 1) { let ls = subStr.character(at: 1); let uc = Int(((hs - 0xd800) * 0x400) + (ls - 0xdc00)) + 0x10000; if (0x1d000 <= uc && uc <= 0x1f77f){ returnValue = true; } } } else if (subStr.length > 1) { let ls = subStr.character(at: 1); if (ls == 0x20e3){ returnValue = true; } } else { // non surrogate if (0x2100 <= hs && hs <= 0x27ff){ returnValue = true; } else if (0x2B05 <= hs && hs <= 0x2b07){ returnValue = true; } else if (0x2934 <= hs && hs <= 0x2935){ returnValue = true; } else if (0x3297 <= hs && hs <= 0x3299){ returnValue = true; } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50){ returnValue = true; } } }; return returnValue; }
3、获取文本的宽度//MARK: - 获取文本的宽度 func getWidthForString(strTitle:String,txtFont:UIFont) -> CGFloat { var fw = CGFloat.init(0) let size = NSString.init(string: strTitle).size(attributes: [ NSFontAttributeName:txtFont ]) fw = size.width return fw }
4、字典转换为Json String/** * 字典转换为JSONString * @parameter dictionary: 字典参数 * @returns: JSONString */ func getJSONStringFromDictionary(dictionary:NSDictionary) -> String { if (!JSONSerialization.isValidJSONObject(dictionary)) { print("无法解析出JSONString") return "" } let data : NSData! = try? JSONSerialization.data(withJSONObject: dictionary,options: []) as NSData! let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue) return JSONString! as String } 5、UISearchBar样式属性修改//MARK: - UISearchBar相关设置 //清除查询框背景 func clearnSearchBarBg(searchView:UISearchBar){ for item:UIView in (searchView.subviews) { for subview:UIView in item.subviews { if subview.isKind(of: UITextField.classForCoder()) { //将搜索文本框的背景去掉 for obj in subview.subviews { obj.removeFromSuperview() } } } } } //设置取消按钮为中文 func setSearchBarCancle(searchView:UISearchBar){ for item:UIView in (searchView.subviews) { for subview:UIView in item.subviews { //更改取消按钮 if subview.isKind(of:UIButton.classForCoder()) { let btnCancle:UIButton = subview as! UIButton //修改文本 btnCancle.setTitle("取消",for: .normal) //修改背景 btnCancle.backgroundColor = UIColor.clear } } } } //设置提示文本字体大小 func setSearchBarFont(searchView:UISearchBar,font:UIFont){ for item:UIView in (searchView.subviews) { for subview:UIView in item.subviews { if subview.isKind(of: UITextField.classForCoder()) { (subview as? UITextField)?.font = font } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- xml – 从Vantage Pro 2气象站发布数据的数据格式是什么?
- ruby – 如何在heroku中使用自己的mysql数据库服务器?
- vue.js将unix时间戳转换为自定义时间格式
- 如何使用Swift中带有typealias的Protocol中的约束引用泛型类
- ios – SceneKit Audio – 如何将SCNPlayer添加到SCNNode并
- support 依赖的冲突的解决思路
- applicationContext.xml 配置文件的存放位置
- SQLite学习笔记之一
- c# – 如何更改哪一天是一周的第一天?
- Oracle 12c 多租户家族(12c 18c 19c) 如何在 PDB 中添加