Zoom UILabel &以正确的大小重新渲染字体

时间:2022-11-03
本文介绍了Zoom UILabel &以正确的大小重新渲染字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个多行 UILabel 我想启用缩放功能.

I have a multi-line UILabel that I want to enable zooming on.

我用 UIScrollView 嵌入了它,并将最小缩放设置为 .25,最大缩放设置为 4.这很好用,但是我的 UILabel 的字体看起来相当粗糙除 1 以外的任何缩放级别.

I embedded it with a UIScrollView and set min zoom to .25 and max zoom to 4. This works well, however my UILabel's font looks rather gross at any zoom level other than 1.

我可以处理这个方法:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale

为了将我的 UILabel 的字体重新调整为更大的大小,但视图仍然被放大,所以看起来总是很糟糕.

in order to re-size the font of my UILabel to something larger, but the view is still zoomed in, so it always looks awful.

有没有办法让标签的文本重新渲染一个我完成缩放的?

Is there any way to make the label's text re-render one I'm done zooming?

重要的是不要丢失用户当前在文本中滚动的位置.

(要了解我的目标,请注意在 Mobile Safari 中,当您缩放时,文本会在一瞬间缩放/消除锯齿,然后在您当前的缩放比例下清晰呈现)

(To get a feel for what I'm going for, notice how in Mobile Safari when you zoom the text is scaled/anti-aliased for a split second then it clears up to render well at your current zoom scale)

推荐答案

UIScrollView 的内置缩放只对您的内容视图应用变换,这会导致缩放因子超过 1.0 时出现模糊.要获得真正清晰的渲染,您需要自己处理缩放.我在 这个答案.

The UIScrollView's built-in scaling only applies a transform to your content view, which results in blurriness at anything above a scale factor of 1.0. For truly sharp rendering, you'll need to handle the scaling yourself. I describe a chunk of the process in this answer.

您需要手动跟踪内容视图的比例因子,然后在 -scrollViewDidEndZooming:withView:atScale: 委托方法中应用该比例.对于您的 UILabel,这将意味着更改字体大小以反映新的比例.

You'll need to keep track of the scale factor of the content view manually, then in the -scrollViewDidEndZooming:withView:atScale: delegate method you'll apply that scale. For your UILabel, that will mean changing the font size to reflect the new scale.

为了保持正确的滚动位置,您需要在上述委托方法中获取 UIScrollView 的 contentOffset 并计算新缩放的 UILabel 中对应的位置.然后设置滚动视图的 contentSize 以匹配 UILabel 的新大小,并使用 -setContentOffset:animated: 设置新计算的内容偏移量(动画设置为 NO).

In order to maintain the correct scroll position, you'll need to grab the contentOffset of the UIScrollView within the above delegate method and calculate what position that corresponds to in the newly scaled UILabel. You then set the contentSize of the scroll view to match the new size of the UILabel and use -setContentOffset:animated: to set the newly calculated content offset (with animated set to NO).

正确计算数学有点棘手,但我在我的一个应用程序中缩放文本时会这样做,这可以在 该应用程序的视频演示(大约在 1/3 处).

It's a little tricky to get the math right, but I do this when scaling text in one of my applications, which can be seen in the video demonstration of that application (at about the 1/3 mark).

这篇关于Zoom UILabel &以正确的大小重新渲染字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:在 iOS 6 上使用自动布局在 ScrollView 中嵌入 ImageView 下一条:在 UIScrollView 上拦截平移手势会中断滚动

相关文章

最新文章