Josh Sherick
Josh Sherick

Reputation: 2161

UITextView textColor automatically becoming the same as the background color

I have some code that creates a bunch of UITextViews and puts them into another view. I change the backgroundColor to alternating colors and always set the textColor to black. They all work out fine, except for the last one that it creates. That one changes it's textColor to whatever the backgroundColor of the text view is and then stops updating it's UI.

If I check the value of textColor of the problematic in GDB, it is black, even though it isn't displayed that way. Then I change it programmatically (push a button, loop through all the created text views and set the textColors all to purple), they all change except for the last one, who's background color is the same as it's text color. Again, when I check in GDB, the value of textColor, it is set at purple, even though this is not reflected on the screen.

WTF?!?!?! Any ideas? Could this just be a bug?

Here's the code I'm using to add the UITextViews. I have UILabels in between so I can get centered text.

UIColor *evenColor = [self RGBColorR:90 G:95 B:90];
UIColor *oddColor = [self RGBColorR:70 G:75 B:70];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    // Loop through adding the buttons.
    for (int i = 0; i < numberOfSections; i++) {
        // Add the label for the actual title of the level.
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, ((i * sectionHeight) - 1), self.levelLablesView.frame.size.width, 30)];
        [label setText:[levels objectAtIndex:i]];
        label.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        if (i % 2 == 0) {
            // It's an even number.
            [label setBackgroundColor:evenColor];
        } else {
            // Its an odd number.
            [label setBackgroundColor:oddColor];
        }
        [self.levelLablesView addSubview:label];
        [label release];

        // Add a scrolling UITextView for the other stuff.
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, ((i * sectionHeight) + 24), self.levelLablesView.frame.size.width, (sectionHeight - 24))];
        [textView setText:[[[dictionary objectForKey:self.chosenCategory]
                            objectForKey:[levels objectAtIndex:i]]
                           objectForKey:@"Description"]];
        [textView setEditable:NO];
        [textView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        if (i % 2 == 0) {
            // It's an even number.
            [textView setBackgroundColor:evenColor];
        } else {
            // Its an odd number.
            [textView setBackgroundColor:oddColor];
        }
        if (i == (numberOfSections-1)) {
            [textView setFrame:CGRectMake(0,((i * sectionHeight) + 24), self.levelLablesView.frame.size.width, ((sectionHeight - 24) + 2))];
        }
        textView.font = [UIFont systemFontOfSize:24];
        [textView setTextColor:[UIColor blackColor]];

        [self.levelLablesView addSubview:textView];
        [textView release];
    }

}

Edit: In case anyone is wondering, I don't set the text color anywhere else in the program by accident. I did a find for "textColor" and the only matches I get are irrelevant.

Upvotes: 0

Views: 1418

Answers (2)

Josh Sherick
Josh Sherick

Reputation: 2161

I fixed this with hacks. I think that this is a bug.

Originally, I added a UIView filled with the background color that I wanted behind the UITextView.

However, I discovered that scrolling the UITextView also changes the background color to the color that it is set as. So I added a BOOL alreadyScrolled; to my .h, added a call to [self scrollLevelLabelsViews] in viewDidAppear: (which first checks to make sure it hasn't alreadyScrolled because viewDidAppear: can get called multiple times if a modal view is presented, for example), and added the following method to scroll the views. I was also having an issue with the last text view not displaying text correctly but neglecting to create it until the alreadyScrolled method after the view has appeared makes it display fine.

The following are the methods I created to scroll the text views down and back up. Just change levelLablesView to the view that contains your textViews, add the method calls and variables I explained in the last paragraph, and it should work for you.

- (void)scrollLevelLabelsViewsBackUp{

    // Scroll all UITextViews in levelLabelsView to the top.
    for (UIView *view in self.levelLablesView.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView *textView = (UITextView *)view;
            [textView scrollRangeToVisible:NSMakeRange(1, 1)];
        }
    }
    alreadyScrolled = YES;

}

- (void)scrollLevelLabelsViews{

   /* Setup last UITextView that displays weirdly if it is set up with the rest of them.
    * ...
    */

    // Scroll all UITextViews in levelLabelsView to the bottom.
    for (UIView *view in self.levelLablesView.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView *textView = (UITextView *)view;
            [textView scrollRangeToVisible:NSMakeRange([[textView text] length] - 2, 1)];
        }
    }

    // In .8 seconds, scroll them back up.
    [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(scrollLevelLabelsViewsBackUp) userInfo:nil repeats:NO];

}

This is also just a cool effect even if you're not having this problem as it lets the user know that there is more text that they can't see and that the text view is scrollable.

Upvotes: 1

JDx
JDx

Reputation: 2655

How is numberOfSections being set?

Maybe try :

for (int i = 0; i <= numberOfSections; i++) {

Upvotes: 0

Related Questions