UILabel Pixelated on Zoom in iOS App

时间:2016-03-17 13:22:17

标签: ios objective-c uiscrollview uilabel pinchzoom

我在'orderby' => 'title', 中添加了UILabel,此视图添加为UIView的子视图,并启用了缩放功能。工作每件事都很好但是当我放大UILabel和其他属性时,这看起来很奇怪。我还添加了以下代码以获得灵活的尺寸。

UIScrollView

上面的视图是`UIView类型。 以下是Label的代码

    userResizableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
userResizableView.autoresizesSubviews = YES;

请给我任何建议如何做好。寻找建议。 感谢

2 个答案:

答案 0 :(得分:0)

也许框架标签使用浮动数字。试试这个强制框架的整数值:

[label setFrame:CGRectIntegral(label.frame)];

答案 1 :(得分:0)

As per my requirement I have added pinch gesture on uilabel like this :

1. Add this line before implementation. 
CG_INLINE CGRect CGRectScale(CGRect rect, CGFloat wScale, CGFloat hScale)
{
    return CGRectMake(rect.origin.x * wScale, rect.origin.y * hScale, rect.size.width * wScale, rect.size.height * hScale);
}

2. Add gesture on label

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidPinch:)];
pinch.delegate =self;
[self addGestureRecognizer:pinch];

3. Add method in your file. Add scrollview bounds instead of self.bounds as I have different scenario in my case.
- (void)viewDidPinch:(UIPinchGestureRecognizer*)sender
{

   CGFloat scale = sender.scale;

    CGRect scaleRect = CGRectScale(self.bounds, scale, scale);

       if (scaleRect.size.width >= 5 && scaleRect.size.height >= 5) {


           [self._label setAttributedText:[self getScaledStringFrom:self._label.attributedText withScale:scale]];

             [self._label setFrame:self.bounds];

    }
     sender.scale=1.0;

}

4. Rewrite text with new scale 
- (NSMutableAttributedString*) getScaledStringFrom:(NSMutableAttributedString*)string withScale:(CGFloat)scale
{
    [string beginEditing];
    [string enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, string.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
        if (value) {
            UIFont *oldFont = (UIFont *)value;
            UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * scale];
            [string removeAttribute:NSFontAttributeName range:range];
            [string addAttribute:NSFontAttributeName value:newFont range:range];
        }
    }];
    [string endEditing];
    return string;
}