How to enlarge dynamically an UILabel (label and font size)?

Im currently working on an iPhone project. I want to enlarge dynamically an UILabel in Objective-C like this:

alt text http://img268.imageshack.us/img268/9683/bildschirmfoto20100323u.png

How is this possible? I thought I have to do it with CoreAnimation, but I didn't worked. Here is the code I tried:

UILabel * fooL = //[…]
fooL.frame = CGRectMake(fooL.frame.origin.x, fooL.frame.origin.y, fooL.frame.size.width, fooL.frame.size.height);   
fooL.font = [UIFont fontWithName:@"Helvetica" size:80];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
fooL.font = [UIFont fontWithName:@"Helvetica" size:144]; //bigger size
fooL.frame = CGRectMake(20 , 44, 728, 167); //bigger frame
[UIView commitAnimations];

The problem with this code is that it doesn't change the fontsize dynamically.


所有你需要做的就是对UILabel对象应用仿射变换。

CGFloat scaleFactor = 2.0f;
label.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor); // Enlarge by a factor of 2.

Scaling your label as suggested by others using the transform property will work great. One thing to keep in mind is that as the label gets larger, the font is not increasing, but just the rendered text, which means it will appear "fuzzier" as it gets larger.


Try this method:

  • (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
  • Parameters: transition A transition to apply to view. Possible values are described in UIViewAnimationTransition.

    view The view to apply the transition to.

    cache If YES, the before and after images of view are rendered once and used to create the frames in the animation. Caching can improve performance but if you set this parameter to YES, you must not update the view or its subviews during the transition. Updating the view and its subviews may interfere with the caching behaviors and cause the view contents to be rendered incorrectly (or in the wrong location) during the animation. You must wait until the transition ends to update the view.

    If NO, the view and its contents must be updated for each frame of the transition animation, which may noticeably affect the frame rate.

    Discussion If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:

    Begin an animation block. Set the transition on the container view. Remove the subview from the container view. Add the new subview to the container view. Commit the animation block.

    链接地址: http://www.djcxy.com/p/45260.html

    上一篇: Git:你怎么使用它?/傻瓜教程

    下一篇: 如何动态放大UILabel(标签和字体大小)?