Swift 常用方法总结
来源:互联网 发布:网络直播发展趋势 编辑:程序博客网 时间:2024/05/23 19:18
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 } } }}
阅读全文
1 0
- Swift 常用方法总结
- swift 字典常用方法
- Swift String常用方法
- 【swift-总结】方法
- Swift字符串常用操作总结
- Swift之NSString常用方法
- Swift String 一些常用方法
- Swift 字典的常用方法
- Swift UIView 常用添加方法
- swift UIView常用添加方法
- Swift ☞ 数组常用方法
- [Swift 开发] GCD常用方法
- swift一些常用的字符串方法
- Swift 3.0中GCD的常用方法
- jquery常用方法总结
- jquery常用方法总结
- luabind常用方法总结
- JQuery常用方法总结
- hadoop 执行 jar文件流程
- Leetcode-14: Longest Common Prefix
- ActiveMQ集群配置及使用
- 配置IBM WebSphere MQ及注意事项
- JDBC学习总结_2_DBUtils
- Swift 常用方法总结
- 我的博客地址
- SVM支撑向量机详解(一)
- ios-网络的基本概念
- 《剑指offer》牛客网java题解-旋转数组的最小数字
- 多线程-线程间通讯
- 压缩问题--实验吧
- Java的优势
- 母函数详解