Swift 常用方法总结

前端之家收集整理的这篇文章主要介绍了Swift 常用方法总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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
            }
        }
    }
}
原文链接:https://www.f2er.com/swift/321328.html

猜你在找的Swift相关文章