Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,706
6,289
I have this code:
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:category ofType:@"txt"];
    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF16BigEndianStringEncoding error:NULL];
    characters = [string componentsSeparatedByString:@"\n"];
    
    [characters enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
     {
         UIButton *key = [UIButton buttonWithType:UIButtonTypeRoundedRect];
         key.frame = CGRectMake((index%6)*(self.frame.size.width/6.0),
                                (floor(index/6.0))*(self.frame.size.width/6.0),
                                self.frame.size.width/6.0,
                                self.frame.size.width/6.0);
         key.titleLabel.text = obj;
         key.titleLabel.textColor = [UIColor blackColor];
         key.titleLabel.font = [UIFont systemFontOfSize:5.0];
         key.titleLabel.frame = CGRectMake(10, 10, 50, 50);
         key.titleLabel.backgroundColor = [UIColor redColor];
         [self addSubview:key];
     }];

What it does is it finds a txt file full of strings. I want all the strings to become titles for a grid of buttons.

I know it's accessing the strings fine, because I had an NSLog mixed in spitting out the strings that it was having as a label for each button. The grid of buttons is also displaying fine.

But the titleLabel is in no way showing up. The red background doesn't show up. The black text doesn't show up. I've tweaked the font size and the titleLabel.frame (including not touching the properties and leaving it as default) a lot but the results are all the same: nothing shows up.

Is there something I'm doing wrong?
 
UIButton's titleLabel is not set via the property but instead via the setTitle:forState: instance method.

Thanks. Seems like I end up having to be told this once a year at least...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.