Zoom UILabel & Re-render font at correct size

2019-01-21 18:02发布

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)

8条回答
啃猪蹄的小仙女
2楼-- · 2019-01-21 18:39

Thanks valvoline & Scrimmers for your answers. My solution was a mix between yours.

Subclass UILabel like this:

UILabelZoomable.h

+ (Class) layerClass;

UILabelZoomable.m

+ (Class) layerClass
{    
    return [CATiledLayer class];
}

Now, when you use your fancy new UILabelZoomable in your app, remember to do this:

YourViewController.m

CATiledLayer *tiledLayer = (CATiledLayer*)textoLabel.layer;
tiledLayer.levelsOfDetail = 10;
tiledLayer.levelsOfDetailBias = 10;

Remember to add the QuartzCore framework!

#import <QuartzCore/QuartzCore.h>

Enjoy sharp and a beautiful rendered text:

[UIView animateWithDuration:1 animations:^
     {
         yourLabel.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(200, 200), CGAffineTransformMakeRotation(1));
     }];
查看更多
The star\"
3楼-- · 2019-01-21 18:41

There is a simpler way to do this..

Replace the layerClass of the UILabel with a CATiledLayer and set the level of detail appropriately.

+ (Class)layerClass
{
    CATiledLayer *layerForView = (CATiledLayer *)self.layer;
    layerForView.levelsOfDetailBias = 2;
    layerForView.levelsOfDetail = 2;
    return [CATiledLayer class];
}

Job done

查看更多
登录 后发表回答