iOS 13 UITextView converting UITextView to image is not working properly. Working fine in below iOS 12

后端 未结 5 1630
鱼传尺愫
鱼传尺愫 2020-12-10 07:28

How to solve this issue in iOS 13? I am taking screenshot of UITextView content size..Till iOS 12 everything is working fine. But issue with iOS 13 onwards it\'s not taking

5条回答
  •  眼角桃花
    2020-12-10 08:23

    Try to use UIlabel or TempView or Third party library Below is Working code

    OPTION 1:- Below code using UILabel

    let SCREEN_WIDTH = UIScreen.main.bounds.size.width
    let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
    func buttonAction() {
        let textView = UITextView()
        textView.frame = CGRect(x: 10, y: 50, width: SCREEN_WIDTH - 20, height: SCREEN_HEIGHT * 2)
    
        let lab = UILabel(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH - 20, height: textView.contentSize.height))
        lab.backgroundColor = UIColor.white
        lab.font = textView.font
        lab.textColor = UIColor.black
        lab.numberOfLines = 0
        lab.text = textView.text
        textView.addSubview(lab)
    
        UIGraphicsBeginImageContextWithOptions(CGSize(width: SCREEN_WIDTH - 20, height: textView.contentSize.height), _: true, _: 1)
        if let context = UIGraphicsGetCurrentContext() {
            lab.layer.render(in: context)
        }
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        textView.frame = CGRect(x: 10, y: 50, width: SCREEN_WIDTH - 20, height: SCREEN_HEIGHT * 2)
        lab.removeFromSuperview()
    
    }
    

    OPTION 2:- Below code using TempView

    let SCREEN_WIDTH = UIScreen.main.bounds.size.width
    let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
    
    func buttonAction() {
       let textView = UITextView()
       textView.frame = CGRect(x: 10, y: 50, width: SCREEN_WIDTH - 20, height: SCREEN_HEIGHT * 2)
       UIGraphicsBeginImageContextWithOptions(CGSize(width: SCREEN_WIDTH - 20, height: textView.contentSize.height), _: true, _: 1)
    
       if #available(iOS 13, *) {
        // iOS 13 (or newer)
        let tempView = UIView(frame: CGRect(x: 0, y: 0, width: textView.contentSize.width, height: textView.contentSize.height))
        tempView.addSubview(textView)
        if let context = UIGraphicsGetCurrentContext() {
            tempView.layer.render(in: context)
        }
        tempView.removeFromSuperview()
        view.addSubview(textView)
     } else {
        // iOS 12 or older
        if let context = UIGraphicsGetCurrentContext() {
            textView.layer.render(in: context)
        }
    }
    

    OPTION 3:- Try to use third party library i have used YYTextView https://github.com/ibireme/YYText

提交回复
热议问题