美文网首页
iOS知识小积

iOS知识小积

作者: MichleMin | 来源:发表于2017-02-18 10:31 被阅读26次

    1.iOS 拨打电话的三种方式 -swift

    // 有提示
     let callWebView = UIWebView()
     callWebView.loadRequest(URLRequest(url:URL(string: "tel:\(10086)")!))
     self.addSubview(callWebView)
     //2.有提示
     UIApplication.shared.openURL(URL(string: "telprompt://10086")!)
    //3.无提示
    UIApplication.shared.openURL(URL(string: "tel://10086")!)
    

    2.iOS 汉字转拼音 -swift

    // 拓展一个String的类方法
    extension String{
        /**
         * 中文转拼音,处理带有多音字
         * polyphoneArr: 需要被处理的多音字数组 
         * Dictionnary<String,String>  value:需要被替换的多音字;key:用于替换的拼音
         */
        func transformMandarinToLatin(_ polyphoneArr: [[String : String]]?)->String{
            var tempStr = self
            if polyphoneArr != nil{
                for polyphone in polyphoneArr!{
                    for (key, value) in polyphone{
                        if self.contains(value){
                            tempStr = tempStr.replacingOccurrences(of: value, with: key)
                        }
                    }
                }
            }
            let mutableStr = NSMutableString(string: tempStr) as CFMutableString
            CFStringTransform(mutableStr, nil, kCFStringTransformToLatin, false) // 转换成带有音调的拼音
            CFStringTransform(mutableStr, nil, kCFStringTransformStripDiacritics, false) // 去掉声调
            tempStr = (mutableStr as String).replacingOccurrences(of: " ", with: "") // 去掉空格
            return tempStr
        }
    }
    // Demo
     let str1 = "长藏"
     let result = str1.transformMandarinToLatin([["chang" : "长"], ["cang": "藏"]])
     print(result) // changcang
    

    3.iOS self.navigationItem.titleView来设置titleview,并且要求达到和屏幕一样宽 -swift

    // 通过重写 UIView 的 setFrame方法实现
        override var frame: CGRect{
            didSet{
                var newFrame = frame
                newFrame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 34)
                super.frame = newFrame
            }
        }
    // Demo
    let titleView = CustomNavView()
    titleView.backgroundColor = UIColor.blue
    self.navigationItem.titleView = titleView
    
    demo.png

    4.iOS计算字符串的长度 -swift

    /** 计算字符串的尺寸 */
     func getStringSize(str: String,font: UIFont)->CGSize{
         let attributes = [NSFontAttributeName: font]
         let size = (str as NSString).size(attributes: attributes)
         return size
    }
    

    5.iOS 生成二维码、条形码 -swift

    // 二维码生成
    func createQRCodeImage(codeString: String,size: CGSize,qrColor:UIColor,bkColor:UIColor,qrImageName: String? = nil)-> UIImage?{
        let stringData = codeString.data(using: String.Encoding.utf8)
        // 创建一个二维码的滤镜
        let qrFilter = CIFilter(name: "CIQRCodeGenerator")
        // 设置二维码包含的信息
        qrFilter?.setValue(stringData, forKey: "inputMessage")
        // 设置二维码的容错率
        qrFilter?.setValue("H", forKey: "inputCorrectionLevel")
        
        //上色
        let colorFilter = CIFilter(name: "CIFalseColor", withInputParameters: ["inputImage":qrFilter!.outputImage!,"inputColor0":CIColor(cgColor: qrColor.cgColor),"inputColor1":CIColor(cgColor: bkColor.cgColor)])
        // 获取生成的二维码图片
        let qrImage = colorFilter!.outputImage
        // 放大二维码图片
        let codeImage = UIImage(ciImage: qrImage!.applying(CGAffineTransform(scaleX: 8, y: 8)))
        // 通常,二维码都是定制的,中间都会放想要表达意思的图片
        if let iconImageName = qrImageName{
            let rect = CGRect(x: 0,y: 0,width: codeImage.size.width,height: codeImage.size.height)
            UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
            codeImage.draw(in: rect)
            let avatarSize = CGSize(width: rect.size.width/4,height: rect.size.height/4)
            let x = (rect.width-avatarSize.width)*0.5
            let y = (rect.height-avatarSize.height)*0.5
            _ = UIImage(named:iconImageName)?.draw(in: CGRect(x: x,y: y,width: avatarSize.width,height: avatarSize.height))
            let resultImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return resultImage
        }
        return codeImage
    }
    
    // 生成条形码
    func createCode128(codeString:String, size:CGSize,qrColor:UIColor,bkColor:UIColor )->UIImage?
    {
        let stringData = codeString.data(using: String.Encoding.utf8)
        
        
        //系统自带能生成的码
        //        CIAztecCodeGenerator 二维码
        //        CICode128BarcodeGenerator 条形码
        //        CIPDF417BarcodeGenerator
        //        CIQRCodeGenerator     二维码
        let qrFilter = CIFilter(name: "CICode128BarcodeGenerator")
        qrFilter?.setDefaults()
        qrFilter?.setValue(stringData, forKey: "inputMessage")
        
        
        
        let outputImage:CIImage? = qrFilter?.outputImage
        let context = CIContext()
        let cgImage = context.createCGImage(outputImage!, from: outputImage!.extent)
        
        var image = UIImage(cgImage: cgImage!, scale: 1.0, orientation: UIImageOrientation.up)
        
        
        // Resize without interpolating
        let scaleRate:CGFloat = 20.0
        
        let width    = image.size.width * scaleRate;
        let height   = image.size.height * scaleRate;
        
        UIGraphicsBeginImageContext(CGSize(width: width,height: height));
        let context1 = UIGraphicsGetCurrentContext();
        context1!.interpolationQuality = CGInterpolationQuality.none;
        image.draw(in: CGRect(x: 0,y: 0,width: width,height: height))
        
        image = UIGraphicsGetImageFromCurrentImageContext()!;
        UIGraphicsEndImageContext();
        
        return image
    }
    

    相关文章

      网友评论

          本文标题:iOS知识小积

          本文链接:https://www.haomeiwen.com/subject/okyrwttx.html