Nash Equilibrium Nash Equilibrium - 5 months ago 36
Swift Question

UITextView in Swift2.2 shows Pixelated text, how do I regain the lost resolution?

Image of the pixelated text in the UITextView

Does anyone have any suggestions? The image of the issue is in the clickable link above.

Code:

struct Views {
static var name_field: UITextView?
}


In the viewDidLoad()

Views.name_field = UITextView(frame: CGRectMake(0, 0, name_field_width, 50))
Views.name_field!.textAlignment = NSTextAlignment.Center
Views.name_field!.font = UIFont.systemFontOfSize(15)
Views.name_field!.autocorrectionType = UITextAutocorrectionType.No
Views.name_field!.keyboardType = UIKeyboardType.Default
Views.name_field!.returnKeyType = .Done
Views.name_field!.delegate = self


Calling this function to style it

styleIt(Views.name_field!)


Adds a bottom border style and then sets the font, etc.

func styleIt(target: UITextView){
target.layer.backgroundColor = UIColor.clearColor().CGColor

let _border = CAShapeLayer()
_border.backgroundColor = UIColor.whiteColor().CGColor
_border.frame = CGRectMake(0, CGRectGetHeight(target.frame) - 1.0, CGRectGetWidth(target.frame), 1.0)

_border.shadowColor = UIColor.whiteColor().CGColor
_border.shadowOffset = CGSize(width: 3, height: 3)
_border.shadowOpacity = 0.23
_border.shadowRadius = 4

target.layer.addSublayer(_border)

target.font = UIFont(name: "ClementePDaa-Hairline", size: 24)
target.textColor = UIColor.whiteColor()
target.textContainerInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
applyPlaceholderStyle(target, placeholderText: _SEARCH_TEXT)

target.returnKeyType = .Done
target.frame = CGRectIntegral(target.frame)

target.layer.shouldRasterize = true
_border.shouldRasterize = true
target.textInputView.layer.shouldRasterize = true
}


This UITextView is a subview of
search_field
which is simply a UIView

search_field!.addSubview(Views.name_field!)

Answer

Your text view is blurry because the frame is using floating numbers. To force integers value for your frame just do :

textView.frame = CGRectIntegral(textView.frame)

Comments