Uitextfield Blurred Text

Uitextfield background color blurry?

I found a solution where you place a view behind the UITextfield, and make it transparent.

let v = UIView()
v.frame = CGRect(x: 30, y: 100, width: 180, height: 30)
let blurEffect = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = v.bounds
blurView.backgroundColor = .clear
v.addSubview(blurView)

let p = UITextField()
p.frame = CGRect(x: 0, y: 0, width: 180, height: 30)
p.layer.isOpaque = true
p.backgroundColor = .clear
v.addSubview(p)

self.view.backgroundColor = .red
self.view.addSubview(v)

This is an example of proposed solution, with background image instead of red color, to emphasize the blur effect

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background") ?? UIImage())

UITextfield blurred text

It sounds (and looks) like you might be piling multiple text fields on top of each other.

UITextField blurred text

OK I'm answering my own question here.

I found a number of references to this bug through Google, but everybody worked around it by playing with font sizes. After much hunting I found this thread that says anti-aliasing is applied when a view's frame contains fractional pixel values, e.g. if you calculate its size as a fraction of the super view.

Sure enough, casting the CGRect values to (int) for the view's frame worked perfectly. So as an example, if you wanted your text field to be centered vertically in the superview, you should use an (int) cast like this:


textFieldWidth = 300;
textFieldHeight = 31;
offsetX = 0;
offsetY = (superview.bounds.size.height - textFieldHeight) / 2;

textField.frame = CGRectMake((int) offsetX,
(int) offsetY,
(int) textFieldWidth,
(int) textFieldHeight);

There is also the CGRectIntegral function that you can use to convert a CGRect to integral values.

UIKit - How to prevent default blur and focus background of UITextField?

Actually we always can inherit UIKit classes and do whatever layout/style we want. Here is very raw demo of how this can be done.

Prepared & tested with Xcode 12.1 / tvOS 14.0

demo2

demo3

So just substitute custom subclass of UITextField class

demo1

class MyTextField: UITextField {

lazy var textLayer = CATextLayer()

override func didMoveToSuperview() {
super.didMoveToSuperview()
layer.backgroundColor = UIColor.clear.cgColor

textLayer.font = self.font
textLayer.fontSize = 36
textLayer.foregroundColor = UIColor.white.cgColor
textLayer.alignmentMode = .center
textLayer.frame = layer.bounds
layer.addSublayer(textLayer)
layer.borderWidth = 2
}

override func layoutSublayers(of layer: CALayer) {
layer.borderColor = self.isFocused ? UIColor.black.cgColor : UIColor.clear.cgColor
textLayer.frame = layer.bounds
textLayer.string = self.text?.isEmpty ?? true ? self.placeholder : self.text
}

override func addSubview(_ view: UIView) {
// blocks standard styling
}

}

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

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

Custom UITextField blurred text

I found a number of references to this bug through Google, but everybody worked around it by playing with font sizes. After much hunting I found this thread that says anti-aliasing is applied when a view's frame contains fractional pixel values, e.g. if you calculate its size as a fraction of the super view.

Sure enough, casting the CGRect values to int for the view's frame worked perfectly. So as an example, if you wanted your text field to be centered vertically in the superview, you should use an int cast like this:

textFieldWidth = 300;
textFieldHeight = 31;
offsetX = 0;
offsetY = (superview.bounds.size.height - textFieldHeight) / 2;

textField.frame = CGRectMake((int) offsetX,
(int) offsetY,
(int) textFieldWidth,
(int) textFieldHeight);

There is also the CGRectIntegral function that you can use to convert a CGRect to integral values.



Related Topics



Leave a reply



Submit