简体   繁体   English

在应用捏手势时更改UILabel的字体大小

[英]Changing Font Size of UILabel on applying Pinch Gesture

I am creating an app which has a UILabel which can be pinched, rotated. 我正在创建一个具有UILabel的应用程序,该应用程序可以被捏,旋转。 When I pinch in the UILabel , it gets pixelated. 当我捏住UILabel ,它将变得像素化。 How can I resolve this? 我该如何解决?

Assuming UILabel is property of your viewController: 假设UILabel是viewController的属性:

CGFloat pinchScale = pinchGesture.scale;
pinchScale = round(pinchScale * 1000) / 1000.0;

if (pinchScale < 1) {
        self.UILabel.font = [UIFont fontWithName:self.UILabel.font.fontName size:(self.UILabel.font.pointSize - pinchScale)];
}
else{
        self.UILabel.font = [UIFont fontWithName:self.UILabel.font.fontName size:(self.UILabel.font.pointSize + pinchScale)];
}

Also, set adjustsFontSizeToFitWidth to YES 另外,将adjustsFontSizeToFitWidth设置为YES

You can get the rounded off scale of the pinchgesture by 您可以通过以下方式获得捏捏手势的四舍五入比例

- (void)pinch:(UIPinchGestureRecognizer *)pinchGesture
   {
     CGFloat pinchScale = pinchGesture.scale;
     pinchScale = round(pinchScale * 1000) / 1000.0;

        YourLabel.font = [UIFont fontWithName:@"your font Name" size:Actual Font size+pinchScale];


   }

Try this may this help. 试试这个可能会有帮助。

I got this to work, try it. 我有这个工作,尝试一下。

pinchScale2 = pinchGesture.scale;
pinchScale2 = round(pinchScale2 * 1000) / 1000.0;

if (pinchScale2 < pinchScale1)
{
        self.UILabel.font = [UIFont fontWithName:self.UILabel.font.fontName size:(self.UILabel.font.pointSize - pinchScale2)];
}
else
{
        self.UILabel.font = [UIFont fontWithName:self.UILabel.font.fontName size:(self.UILabel.font.pointSize + pinchScale2)];
}
pinchScale1 = pinchScale2;

I got this to work, try it. 我有这个工作,尝试一下。

CGFloat maxFontSize;
NSString* string = pTextLabel.text;

UIFont* pfont = [UIFont fontWithName:[pTextLabel font].fontName size:600];
[string sizeWithFont:pfont minFontSize:3 actualFontSize:&maxFontSize forWidth:pTextLabel.frame.size.width - 1 lineBreakMode: UILineBreakModeWordWrap];


UIFont* pStampFont = [UIFont fontWithName:[pTextLabel font].fontName size:maxFontSize];
[pTextLabel setFont:pStampFont];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM