drawAtPoint:和drawInRect:模糊文本

发布于 2024-07-14 20:53:34 字数 677 浏览 7 评论 0原文

当使用 drawAtPoint:、drawInRect: 绘制字符串,甚至设置 UILabels 的文本属性时 - 文本有时会显得稍微模糊。

我倾向于在大多数地方使用 Helvetica,并且我注意到特定的字体大小会导致模拟器和设备上出现一定程度的模糊。

例如:

UIFont *labelFont = [UIFont fontWithName:@"Helvetica-Bold" size:12];

会导致生成的标签的文本稍微模糊。

UIFont *labelFont = [UIFont fontWithName:@"Helvetica-Bold" size:13];

结果是清晰的文本。

我的问题是为什么会出现这种情况? 这只是为字体选择最佳字体大小的问题吗? 如果是这样,最佳字体大小是多少?

更新:看来也许不是字体大小导致了模糊。 矩形的中心可能是一个小数点。 这是我在 Apple 开发者论坛上找到的评论:

检查位置。 它很可能在一个 分数像素。 将中心更改为 整数值。

我对所有点的值进行了四舍五入,但仍有一些地方的文本仍然模糊。 以前有人遇到过这个问题吗?

When drawing strings using drawAtPoint:, drawInRect: and even setting the text property of UILabels - the text can sometimes appear slightly blurry.

I tend to use Helvetica in most places, and I notice that specific font sizes cause some level of blurriness to occur, both in the simulator and on the device.

For example:

UIFont *labelFont = [UIFont fontWithName:@"Helvetica-Bold" size:12];

Will cause the resulting label to have slightly blurry text.

UIFont *labelFont = [UIFont fontWithName:@"Helvetica-Bold" size:13];

Results in crisp text.

My question is why does this occur? And is it just a matter of selecting an optimal font size for a typeface? If so, what are the optimal font sizes?

UPDATE: It seems that perhaps it is not the font size that is causing the blurriness. It may be that the center of the rect is a fractional point. Here is a comment I found on the Apple dev forums:

Check the position. It's likely on a
fractional pixel. Change center to be
integer value.

I rounded off the values of all my points, but there are still places where text remains blurry. Has anyone come across this issue before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凡间太子 2024-07-21 20:53:34

我已经解决了这个问题。

只需确保您正在绘制的点或矩形不会出现在小数像素上即可。

NSLog(@"%@", NSStringFromCGRect(theRect)) 来确定在分数像素上绘制哪个点。 然后在该点调用round()

I have resolved this.

Just make sure that the point or rect in which you are drawing does not occur on a fractional pixel.

I.e. NSLog(@"%@", NSStringFromCGRect(theRect)) to determine which point is being drawn on a fractional pixel. Then call round() on that point.

你丑哭了我 2024-07-21 20:53:34

您可能想看看 NSIntegralRect(),它可以满足您的需求。

You might want to look at NSIntegralRect(), it does what you want.

妳是的陽光 2024-07-21 20:53:34

如果这是不正确的,请原谅我的无知,我对 iPhone 或 Cocoa 一无所知。

如果您要求文本在矩形中居中,您可能还需要确保矩形的宽度和/或高度是偶数。

Pardon my ignorance if this is incorrect, I know nothing about iPhone or Cocoa.

If you're asking for the text to be centered in the rect, you might also need to make sure the width and/or height of the rect is an even number.

灼痛 2024-07-21 20:53:34

我也遇到过这个问题,但我的解决方案不同,可能会对某人有所帮助。
我的问题是通过 TouchesBegan 更改 UIView 对象的大小后文本模糊
CGAffineTransformMakeScale,然后返回到 TouchesEnded 中的 CGAffineTransformIdentity

我尝试了不同的文本大小以及 x 和 y 中心点的舍入,但都不起作用。
我的问题的解决方案是对我的 UIView 的宽度和高度使用均匀的尺寸!

希望这可以帮助 ....

I have had this problem too, but my solution is different and may help someone.
My problem was text blur after changing the size of a UIView object thru TouchesBegan
and CGAffineTransformMakeScale, then back to CGAffineTransformIdentity in TouchesEnded.

I tried both the varying text size and rounding of x and y center points but neither worked.
The solution for my problem was to use even sizes for the width and height of my UIView !!

Hope this helps ....

想你只要分分秒秒 2024-07-21 20:53:34

根据我的实验,某些字体在某些尺寸下无法清晰呈现。 例如 Helvetica-Bold 不会渲染“.” 在 16.0f 时效果很好,但在 18.0f 时它们很清晰。 (如果你仔细观察,“.”的顶部像素是模糊的。)

在我注意到这一点之后,每次看到那个 UIView 时我都会感到厌烦,因为它是动态渲染的。

From my experiments, some fonts don't render clearly at certain sizes. e.g. Helvetica-Bold doesn't render "." well at 16.0f, but they're clear at 18.0f. (If you look closely, the top pixel of the "." is blurry.)

After I noticed that, I've been irked every time I see that UIView, since it's rendered dynamically.

新人笑 2024-07-21 20:53:34

就我而言,我在自定义 CALayer 上绘制文本,结果非常模糊。 我通过设置 contentScale 为适当的值解决了这个问题:

Objective-C:

layer.contentsScale = [UIScreen mainScreen].scale;

Swift:

layer.contentsScale = UIScreen.main.scale

In my case, I drew text on a custom CALayer and turned out to be pretty blurry. I solved it by setting contentScale with appropriate value:

Objective-C:

layer.contentsScale = [UIScreen mainScreen].scale;

Swift:

layer.contentsScale = UIScreen.main.scale
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文