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

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)

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:
you can set the rasterizationScale any number between 0 and UIScreen.main.scale

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.

Related

How to get cornerRadius of UITextField with roundedRect style

I am injecting a UIVisualEffectView (UIBlurEffect) into a UITextField which has a roundedRect border style.
Problem is that the UIVisualEffectView is a solid rectangle and I would like it to be "clipped" by its parent. Or to apply a cornerRadius which is the same has the UITextField, but when I access this value, it is always 0.
Here is the visual thing where you can see that the UITextField has rounded corners but not the blurView:
And here is the code:
let blurView = UIVisualEffectView(effect: blurEffect)
textField.insertSubview(blurView, at: 0)
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Note: I can always set manually the cornerRadius of the blurView, but I would like a "safe" dynamic version
Try this:
textField.layer.cornerRadius = 5.0
textField.layer.masksToBounds = true
[I think the best way to set corner radius is:
]1

Button blur does not work

How can I make a button which get blurred? Behind the button is a image.
This is my code:
let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.regular))
blur.frame = cell.lvlBut.bounds
blur.isUserInteractionEnabled = false
blur.layer.cornerRadius = 0.5 * cell.lvlBut.bounds.size.width
blur.clipsToBounds = true
cell.lvlBut.insertSubview(blur, at: 0)
The blur does not work perfectly, see image below.
Just set the button background color and its opacity according to your requirement.

Transparent blurred view is not working

I'd like to apply layer on top of ImageView that slightly blurrs it.
I put a view on top of imageView, subclass it as follows:
class BlurredView: UIView {
override func draw(_ rect: CGRect) {
self.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.backgroundColor = UIColor.clear
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
}
}
The result is quite different from what I expected.
How to customize it to make this blur much lighter, so the picture still shows some details?
As https://stackoverflow.com/users/1387306/chrstpsln mentioned in the comment, this can be achieved by changing the alpha of the UIVisualEffectView.
As a bonus, you can add a fade animation to make the transition feel smoother.
Heres an example of how you can add this effect to any viewController through an extension:
extension UIViewController {
func addBlurEffect() {
let blurEffect = UIBlurEffect(style: .light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = UIScreen.main.bounds
blurVisualEffectView.alpha = 0.1
// Bonus animation - Or just set the alpha higher
UIView.animate(withDuration: 0.3) {
blurVisualEffectView.alpha = 0.90
}
view.addSubview(blurVisualEffectView)
}
}
Usage:
let viewController = UIViewController()
viewController.addBlurEffect()
You can't change the blur amount, the only thing you can do is to change the blur effect style:
UIBlurEffectStyle.light
UIBlurEffectStyle.extraLight
UIBlurEffectStyle.dark
Try to change the blur effect alpha. Also, since your image is light, you should use the light style and not the extra light in order to not lose details.
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
blurEffect.alpha = 0.95

Change color of the shadow in background

I`ve a App like the iOS 10 Maps-App or maybe like the Home-Screen with Control Center. For example take a look at the Picture. If you open the Control Center the shadow of the background changes to black & transparent. How can i access this layer or subview and change the color from this shadow to another in my App. Maybe to red or white?
EDIT:
Maybe directly with the Framework.
https://github.com/iosphere/ISHPullUp
You can use UIVisualEffectView like this:
Obj-C:
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];//SELECT STYLE OF YOUR CHOICE
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = self.view.bounds;//view Will be YOUR_VIEW
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:blurEffectView];//This add Blur view to your view
Swift:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurEffectView)
Sometimes open your eyes and you will find what you want. 😕 If you using the same Framework there is one variable you can change. In the sample of the Framework you can got to the "BottomVC" and add the following line to your viewDidLoad().
weak var pullUpController: ISHPullUpViewController!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
topView.addGestureRecognizer(tapGesture)
handleView.arrowSize = CGSize(width: 1 , height: 2)
//This line change the default 40% black to a 70% white.
pullUpController.dimmingColor = UIColor.white().withAlphaComponent(0.7)
}
The "Shadow" is just a normal View. Here you can see how the author has implemented this.

Blur view with fade in edge effect - iOS

As you can see image below I want to create blur view with fade in edge effect. Does someone write me codes of how to implement this in ios?
Thanks
please take a look at this image - marked in red rectangle
let blurEffect = UIBlurEffect(style: .Light)
let imageView = UIImageView(image: UIImage(named:))//your ImageView
let visualEffectView = UIVisualEffectView(effect: blurEffect)
imageView.addSubview(visualEffectView)
You might need to change the frame of the visualEffectView to cover your image but this is the basics of it.

Resources