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

856 Views Asked by At

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

2

There are 2 best solutions below

6
Mojtaba Hosseini On BEST ANSWER

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

0
ChrisTheGreat On

You could try something like this:

let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = cell.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
cell.view.addSubview(blurEffectView)

Adding a UIVisualEffectView with blur effect on top of your table view cell.