Swift实战小结

2020-04-14  本文已影响0人  纳兰沫

动态改变状态栏字体颜色

 var changeStatusBarStyle: UIStatusBarStyle = .lightContent {
        didSet{
           setNeedsStatusBarAppearanceUpdate()
        }
 }
    
override var preferredStatusBarStyle: UIStatusBarStyle {
        return changeStatusBarStyle
}

图片设置拉伸

设置为ver。。 然后下面的设置为setch  同时 图片的线放在一起

当自定义navigationBar时

需要设置固有大小 和 navigationBar 的frame
   /// 固有的大小
    override var intrinsicContentSize: CGSize {
        return UILayoutFittingExpandedSize
    }
    
    /// 重写 frame
    override var frame: CGRect {
        didSet {
            super.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 44)
        }
    }

下载图片

import Photos
 func download() {
        //下载图片
        ImageDownloader.default.downloadImage(with: URL(string: "")!, retrieveImageTask: nil, options: nil, progressBlock: { (receivedSize, totalSize) in
            let progress = Float(receivedSize) / Float(totalSize)
            SVProgressHUD.showProgress(progress)
            SVProgressHUD.setBackgroundColor(.clear)
            SVProgressHUD.setForegroundColor(.white)
        }) { (image, errror, imageURL, data) in
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAsset(from: image!)
            }) { (success, error) in
                if success {
                    //保存成功
                }
            }
        }
    }

文字中含有图片转换为真正的表情

struct Emoji {
        var id:String?
        var png:String?
        var name: String?
    }
    var emojis = [Emoji]()
    
    func showEmoji(content: String, font: UIFont) -> NSMutableAttributedString {
        //将content转成attributeString
        let attributeString = NSMutableAttributedString(string: content)
        //emoji正则表达式
        let emojiPattern = "\\[.*?\\]"
        //创建正则表达式对象 匹配empji表情
        let regex = try! NSRegularExpression(pattern: emojiPattern, options: [])
        //开始匹配 返回结果
        let resuls = regex.matches(in: content, options: [], range: NSRange(location: 0, length: content.count))
        if resuls.count != 0 {
            //倒序遍历 从最后一个开始替换 如果从第一个就开始替换 字符串的长度就发生了变化
            for index in stride(from: resuls.count - 1, through: 0, by: -1 ){
                //取出结果的范围
                let result = resuls[index]
                //取出emoji的名字
                let emojiName = (content as NSString).substring(with: result.range)
                let attachment = NSTextAttachment()
                //取出对应的emoji模型
                let emoji = emojis.filter { $0.name == emojiName }.first!
                //设置图片
                attachment.image = UIImage(named: emoji.png!)
                //设置大小
                attachment.bounds = CGRect(x: 0, y: -4, width: font.lineHeight, height: font.lineHeight)
                let attributeImageString = NSAttributedString(attachment: attachment)
                //将图片替换为文字
                attributeString.replaceCharacters(in: result.range, with: attributeImageString)
            }
        }
        return attributeString
    }
上一篇下一篇

猜你喜欢

热点阅读