How to prevent bold images with UIImageRenderingModeAlwaysTemplate

后端 未结 4 1913
野性不改
野性不改 2020-12-31 19:30

My application has a toolbar with image buttons on them (subclass of UIButton); when the user switches on the \"Bold text\" accessibility option, not only the text becomes b

4条回答
  •  旧时难觅i
    2020-12-31 19:33

    This is a swift 3 version of rufel Answer ,

    extension UIImageView {
    fileprivate func tintImage(color: UIColor){
        guard  let _image = image else { return }
        let rect  = CGRect(x: 0.0, y: 0.0, width: _image.size.width  , height: _image.size.height  )
        UIGraphicsBeginImageContextWithOptions(_image.size , false, _image.scale)
        color.set()
        UIRectFill(rect)
        _image.draw(in: rect, blendMode: CGBlendMode.destinationIn, alpha: 1.0)
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    }
    

    OR

    extension UIImage {
    static func imageTinted(image: UIImage?, color: UIColor) -> UIImage? {
        let rect  = CGRect(x: 0.0, y: 0.0, width: image?.size.width ?? 0.0, height: image?.size.height ?? 0.0)
        UIGraphicsBeginImageContextWithOptions(image?.size ?? CGSize.zero, false, image?.scale ?? 2.0)
        color.set()
        UIRectFill(rect)
        image?.draw(in: rect, blendMode: CGBlendMode.destinationIn, alpha: 1.0)
        let tinted = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        return tinted;
    }
    }
    

提交回复
热议问题