GithubHelp home page GithubHelp logo

wentaozone / arlabel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ivankovacevic/arlabel

0.0 2.0 0.0 64 KB

Subclassed version of UILabel that automatically adjusts label font size to fit its frame, specifically designed to work good with animations.

arlabel's Introduction

ARLabel - Autoresizing Label

Ever had one of the following problems with UILabel?

  • Unable to determine the correct font size to fit width and HEIGHT
  • UILabel adjustsFontSizeToFitWidth does not work as expected
  • Could not animate font size change

That is where ARLabel comes in. Forget font size altogether! ARLabel works automatically by setting its font size to fit its frame. So working with ARLabel is more like working with plain UIView. Just set the frame to desired size and forget everything else. You want animated font size change? Just set a different frame inside an UIView animation block.

ScreenShot

How does it work?

The core functionality revolves around calling the CGAffineTransformScale function, which transforms the size of underlying layer altogether. Enlarging would however produce blurry text that's why there is a nice property called enlargedSize which you can set upfront to the expected size that you are going to enlarge your label. What happens in the background is that the label is immediately created with that size and transformed(shrunk) to current frame size. Then when you enlarge it, it is actually transformed back to its original state/size. It's a sort of reversed logic :)

Beside that there is a bunch of other small tricks included in the implementation that handle all the weird border cases

What's with all the comments?!

Well I probably did exagerate a bit with the comments in the files. However I did not want to wonder again why I did something the way I did, that's why I wrote every detail down. The comments in the .h file can help you understand all the extra properties that you can use.

Requirements

Beside the obvious, that you need to include ARLabel .h and .m files in your project also do this: Include QuartzCore.framework under "Link Binary With Libraries" in your project settings.

Example usage

Basically you can use it as you would use UILabel anyway. All the properties that ARLabel implements are optional. However you would want to set enlargedSize if you want nice looking text.

ARLabel *testLabel = [[ARLabel alloc] initWithFrame:CGRectMake(110, 100, 100, 50)];
testLabel.text = @"Test";
testLabel.enlargedSize = CGSizeMake(200, 100);

[self.view addSubview:testLabel];

[UIView animateWithDuration:3.0
                      delay:5.0
                    options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^{
                                 testLabel.frame = CGRectMake(60, 200, 200, 100);
                             }
                 completion:nil];

arlabel's People

Contributors

ivankovacevic avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.