Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Blurry UILabel #196

Open
moshegutman opened this issue Apr 4, 2018 · 3 comments
Open

Blurry UILabel #196

moshegutman opened this issue Apr 4, 2018 · 3 comments
Labels

Comments

@moshegutman
Copy link

When using a UILabel and the alignment frame isn't placed on an integer value, the text can appear blurry.

I solved this by adjusting the return CGRect inside the Alignment constructor. I added a call to .integral

public init(vertical: Vertical, horizontal: Horizontal) {
        self.aligner = { (size: CGSize, rect: CGRect) -> CGRect in
            let (x, width) = horizontal.align(length: size.width, availableLength: rect.width, offset: rect.origin.x)
            let (y, height) = vertical.align(length: size.height, availableLength: rect.height, offset: rect.origin.y)
            return CGRect(x: x, y: y, width: width, height: height).integral
        }
    }

Not sure if this should be classified as a bug

@moshegutman
Copy link
Author

A reference to a similar issue on StackOverflow:
https://stackoverflow.com/questions/8065892/blurry-uilabel-when-added-programmatically

@staguer staguer added the defect label Apr 8, 2018
@staguer
Copy link
Contributor

staguer commented Apr 8, 2018

I think this qualifies as a bug, thanks for reporting, @moshegutman .
I noticed that we have roundedUpToFractionalPoint in CGFloatExtension.swift, that would make smaller adjustments to the rectangles.
Another issue to consider is whether two adjacent rectangles that are adjusted to fall on pixel boundaries do indeed remain adjacent after the adjustment to make sure that a background color doesn't start showing through underneath.

@moshegutman
Copy link
Author

Following your advice, I went with this:

public init(vertical: Vertical, horizontal: Horizontal) {
        self.aligner = { (size: CGSize, rect: CGRect) -> CGRect in
            let (x, width) = horizontal.align(length: size.width, availableLength: rect.width, offset: rect.origin.x)
            let (y, height) = vertical.align(length: size.height, availableLength: rect.height, offset: rect.origin.y)
            return CGRect(x: x.roundedToFractionalPoint, y: y.roundedToFractionalPoint, width: width.roundedToFractionalPoint, height: height.roundedToFractionalPoint)
        }
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants