Search code examples
iosswiftuitextviewblurrycatextlayer

How to reduce the quality of the text inside UITextView with Swift


I'm trying to reduce the quality of a message before its being sent, for example if the connect is poor I want to blur out the text in the UITextView and if connection improves or internet comes back, then remove the blur and show the normal text in the UITextView. I have tried using CATextLayer doesn't work. Is there a way I can achieve this?

cell.messageTextView.text = theText

let textLayer = CATextLayer()
textLayer.string = cell.messageTextView.text
textLayer.contentsScale = 0.2
cell.messageTextView.layer.addSublayer(textLayer)

enter image description here


Solution

  • You can drop the quality of the layer by making it rasterized and reducing the scale of it:

    let badQualityRatio: CGFloat = 4
    textView.layer.shouldRasterize = true
    textView.layer.rasterizationScale = UIScreen.main.scale/badQualityRatio
    

    Result:

    Result

    you can set the rasterizationScale any number between 0 and UIScreen.main.scale