Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to i increase the width of a label with code? IOS objective c

I have a label which will act as a percentage bar. Each time i click the screen i will increase its width. The label is black and above a cream label of equal height so it will look as though its a percentage bar. This is my code which im executing but it does nothing to the label width in the view.

    percentage.frame = CGRectMake(
                                  percentage.frame.origin.x,
                                  percentage.frame.origin.y,
                                  percentage.frame.size.width + 4.41,
                                  percentage.frame.size.height
                                  );

So the issue is that a UILabel's frame is read only, so you can only use setters and getters to change the frame. Now the question I have: Is the percentage a point increase, meaning each click will increase the width by a constant point?
Assuming your percentage increase is an integer, say value 1 (for 1 percentage), then you would want to create a float that represents the total size of the label when it's at 100% and divide that by the number of percentage points.
If so, what you should do is:

dispatch_async(dispatch_get_main_queue(), ^{
      CGFloat increaseUnit = yourFullLabelSize / 100;
      CGRect percentageFrame = percentage.frame;
      percentageFrame.size.width += increaseUnit;
      [percentage setFrame:percentageFrame];  
});

This should all be done inside your IBAction method for the user tapping the screen.