I have a custom UIView with a UILabel and a UIImageView subview. (tried using UIImageView subclass aswell). I assign an image to it and add the view to the screen. I wrote a function which adds the amount of LetterBoxes to the screen (my custom class):
- (void)drawBoxesForWord:(NSString *)word {
if(boxesContainer == nil)
{
/*
Create a container for the LetterBoxes (animation purposes)
*/
boxesContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 205, 320, 50)];
[self.view addSubview:boxesContainer];
}
/*
Calculate width of letterboxes
*/
NSInteger numberOfCharacters = [word length];
CGFloat totalWidth = numberOfCharacters * 28 + (numberOfCharacters - 1) * 3;
CGFloat leftCap = (320 - totalWidth) / 2;
[letters removeAllObjects];
/*
Draw the boxes to the screen
*/
for (int i = 0; i < numberOfCharacters; i++) {
LetterBox *letter = [[LetterBox alloc] initWithFrame:CGRectMake(leftCap + i * 31 , 0, 28, 40)];
[letters addObject:letter];
[boxesContainer addSubview:letter];
[letter release];
}}
This gives me the image below:
http://www.imgdumper.nl/uploads2/4ba3b2c72bb99/4ba3b2c72abfd-Goed.png
But sometimes it gives me this:
imgdumper.nl/uploads2/4ba3b2d888226/4ba3b2d88728a-Fout.png
I add them to the same boxesContainer but they first remove themselves from the superview, so it's not like you see them double or something.
What I find weird is that they are all good or all bad.. This is the init function for my LetterBox:
if (self == [super initWithFrame:aRect]) {
/*
Create the box image with same frame
*/
boxImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
boxImage.contentMode = UIViewContentModeScaleAspectFit;
boxImage.image = [UIImage imageNamed:@"SpaceOpen.png"];
[self addSubview:boxImage];
/*
Create the label with same frame
*/
letterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
letterLabel.backgroundColor = [UIColor clearColor];
letterLabel.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:26];
letterLabel.textColor = [UIColor blackColor];
letterLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:letterLabel];
}
return self;}
Does anyone have an idea why this could be? I'd rather have them display correctly every time :)