iPhone app development: Fade in a label with randomly placed text

With the latest app I am working on I am populating a label with text after a button click (ooohh so exciting), and rather than have it just plop in, I wanted the text to fade in and fade out. Well I haven’t gotten it to fade out first, then fade back in after the text has been placed – but it does fade in. When the button is pressed the text is removed, then the label is set to alpha of 0, then the new text fades in.

//set text to transparent
statusText.alpha = 0;
statusText.transform = CGAffineTransformIdentity;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
//makes text fade in 100%
statusText.alpha = 1;
[UIView commitAnimations];

It’s pretty straight-forward too. After connecting the label to the statusText outlet in Interface Builder, I simply use that outlet to perform the fade from within the buttonPressed action in the view controller file. As long as you have the CoreGraphics framework imported into your project you are good to go. The next bit is a bit more complex, but not really by much.

The text I am placing is set randomly from a .plist file. First you need to set up your viewcontroller.h file with something like:

@interface FirstViewController : UIViewController {
    IBOutlet    UILabel     *statusText;
    IBOutlet    UIButton    *changeTextBtn;

    NSDictionary *textList;
    NSArray *categories;
    NSArray *textData;

@property (retain, nonatomic) UILabel *statusText;
@property (retain, nonatomic) NSDictionary *textList;
@property (retain, nonatomic) NSArray *categories;
@property (retain, nonatomic) NSArray *textData;
@property (retain, nonatomic) UIButton *changeTextBtn;

- (IBAction)buttonPressed:(id)sender;


Here, setting up the dictionary for the text strings is the most important. In the .m file is where we get the text to be placed randomly. Using the viewDidLoad function, set the following:

NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"TextList" ofType:@"plist"];

NSDictionary *dictionary = [[NSDictionary alloc] 
self.textList = dictionary;
[dictionary release];

NSArray *components = [self.textList allKeys];
NSArray *sorted = [components 
self.categories = sorted;

NSString *selectedCategory = [self.categories objectAtIndex:1];
NSArray *array = [textList objectForKey:selectedCategory];
self.excuses = array;

Then place the new random text string using the same buttonPressed function where we set the fade:

int randomNumber = random() % [self.textData count];   
NSString *nextText = [self.excuses objectAtIndex:randomNumber];   
NSString *newText = [[NSString alloc] initWithFormat:@"%@", nextText];
statusText.text = newText;
[newText release];

That’s pretty much it. Some of the stuff in the viewDidLoad function you might not need, but I wanted to sort by categories, then sort by the textListing in each of those categories as well. The next thing on my plate is to get the views to flip right then flip left as the user switches between views using the tabbar.