加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读