I have a multi-line UILabel
that I want to enable zooming on.
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.
I can handle this method:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
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?
It is important that the users current scrolled position in the text not be lost.
(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)
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.
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.
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).
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).
iOS 4+
If this gets too intensive and performance is slow, just try setting the contentScaleFactor for only your labels.
I implemented Scrimmers's solution by subclassing UILabel as DetailedUILabel with overriding methods like this;
import QuartzCore
override init method, initWithFrame whichever you want.
and layerClass, class method.
I tested JEzu's code in iOS 5.1 and 6 beta, it is working for a const text label, but cause other normal label require updating text content fail, i.e. the text is not changed...
I subclass the UILabel as ZoomableLabel with JEzu's answer, and apply the class as the label's custom class and it works fine.
the correct way to substitute a CALayer with a CATiledLayer is as follow:
apart, you've to set your bias detail and whatever you need.
Here is something that I have come up with:
Updated for Swift 4.0:
And also remember that the label should be large enough to show the whole text. It sometimes takes a tiny bit to redraw, but this method is fairly simple.