Wednesday, 24 August 2011

Don't always need an UILabel to display a NSString

I always thought to display a NSString on a view, you need to create a UILabel. But from Apple's "iOS Development Guide", looks like this might not be the case. The "Hello World" example in Page 23 of the guide shows some code (Listing 1-1) as below to display a NSString directly using "drawAtPoint", which is quite interesting...

- (void)drawRect:(CGRect) rect { 
    NSString *hello = @"Hello, World!"
    CGPoint location = CGPointMake(10, 20); 
    UIFont *font = [UIFont systemFontOfSize:24.0]; 
    [[UIColor whiteColor] set]; 
    [hello drawAtPoint:location withFont:font];
}

I guess the downside of doing it like this is, you can't reference back to the same NSString later to perform further actions, such as change content, change location, hide the text, ...etc.

No comments:

Post a Comment