UITextView Content Disappears

Discussion in 'iOS Programming' started by harryslotwiner, Apr 14, 2012.

  1. harryslotwiner macrumors newbie

    Mar 28, 2012
    I have been looking into this for the last couple of days and it would seem that I have run into a bug.

    I have 20 UITextViews being created programmatically. There are 4 text views on the screen at a given time. When you gesture upwards they all move upwards bringing the views currently visible off the screen and 4 that were below the screen onto the screen.

    The problem is that it some of the text views show no content however it is there and you can prove so by tapping and holding over the text view and it will define the invisible word under your finger.

    I have tried calling
    - (void)setNeedsDisplay
    for all text views coming onto the screen but evidently there's something wrong with the drawing cycle because I guess it thinks they don't need to be redrawn. I have also tried:
    textview.text = textview.text;
    So the content is there, it's just invisible. Does anyone know how to fix this or is it just a bug?
  2. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    Not sure if that is the issue, aren't you sure it's the text color value?
    Because I've done thousands of such things, and never had any issue with it :)
  3. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    It's not the text color because if I highlight the text to either "Select All" or "Copy" of "Define" if I very quickly expand one of the anchors for the text selected forcing a redraw then the text suddenly appears.

    Also I have a grainy background so any solid color would at least slightly stick out.
  4. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    Here are some more details:

    - All the text views on the screen at load display their content perfectly.
    - Only some text views don't have there content on them when they come onto the screen and it appears random as to which ones.
    - And just in case here's the code that I am using to create them:
                        UITextView *textView = [[UITextView alloc] initWithFrame:textViewPos1.frame];
                        [textView setFont: [UIFont fontWithName:@"Georgia" size:17]];
                        textView.center = CGPointMake(textView.center.x, textView.center.y + (768 * row));
                        textView.text = [summary stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
                        textView.backgroundColor = [UIColor clearColor];
                        textView.hidden = NO;
                        textView.editable = NO;
                        textView.scrollEnabled = NO;
                        textView.clearsContextBeforeDrawing = YES;
                        [self.view addSubview:textView];
    - Also if I enable scrolling when you scroll on a text view that's content is not visible then it become visible.
  5. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    textView.clearsContextBeforeDrawing = YES;

    Whats that for, i have never used that before ;o
  6. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    Honestly I don't know, I've never used it either. I put it in there thinking it might help but it didn't so I took it out after I posted this.
  7. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    So I made the text views slowly animate to there positions and I watched them and it looks like no matter whether or not they start off on the screen some of them don't have there content from the start. Which makes me think I have make some small mistake somewhere however I can't understand where.

    I'm beginning to get desperate, maybe someone can find something wrong in this code.

    This is the complete code for displaying all content for an article (This is an RSS Parser):
    UILabel *articleTitle = [[UILabel alloc] initWithFrame:articlePos1Title.frame];
                        articleTitle.center = CGPointMake(articleTitle.center.x, articleTitle.center.y + (768 * row));
                        [articleTitle setFont: [UIFont fontWithName:@"Georgia" size:22]];
                        articleTitle.backgroundColor = [UIColor clearColor];
                        articleTitle.text = currentTitle;
                        articleTitle.alpha = 1;
                        articleTitle.hidden = NO;
                        UITextView *articleBody = [[UITextView alloc] initWithFrame:articlePos1Body.frame];
                        articleBody.center = CGPointMake(articleBody.center.x, articleBody.center.y + (768 * row));
                        [articleBody setFont: [UIFont fontWithName:@"Georgia" size:17]];
                        articleBody.text = [currentSummary stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
                        articleBody.backgroundColor = [UIColor clearColor];
                        articleBody.alpha = 0;
                        articleBody.hidden = NO;
                        articleBody.editable = NO;
                        articleBody.scrollEnabled = NO;
                        UIImageView *articleImage = [[UIImageView alloc] initWithFrame:articlePos1Image.frame];
                        articleImage.center = CGPointMake(articleImage.center.x, articleImage.center.y + (768 * row));
                        articleImage.image = [UIImage imageNamed:@"loadingImage.png"];
                        [imageURLArray insertObject:currentImage atIndex:0];
                        articleImage.backgroundColor = [UIColor clearColor];
                        articleImage.alpha = 0;
                        articleImage.hidden = NO;
                        articleImage.clipsToBounds = YES;
                        articleImage.contentMode = UIViewContentModeScaleAspectFill;
                        UIImageView *articleTapZone = [[UIImageView alloc] initWithFrame:articlePos1Zone.frame];
                        articleTapZone.center = CGPointMake(articleTapZone.center.x, articleTapZone.center.y + (768 * row));
                        articleTapZone.backgroundColor = [UIColor clearColor];
                        articleTapZone.alpha = 0;
                        articleTapZone.hidden = NO;
                        UIImageView *divider = [[UIImageView alloc] initWithFrame:dividerMiddle.frame];
                        divider.center = CGPointMake(divider.center.x, divider.center.y + (768 * row));
                        divider.backgroundColor = [UIColor grayColor];
                        divider.alpha = 0;
                        divider.hidden = NO;
                        [dividerArray insertObject:divider atIndex:0];
                        [self.view addSubview:divider];
                        [linkArray insertObject:currentLink atIndex:0];
                        [idArray insertObject:currentID atIndex:0];
                        [self.view addSubview:articleTitle];
                        [self.view addSubview:articleImage];
                        [self.view addSubview:articleTapZone];
                        [self.view addSubview:articleBody];
                        [UIView beginAnimations:nil context:nil];
                        [UIView setAnimationDuration:2];
                        articleTitle.alpha = 1;
                        articleBody.alpha = .7;
                        articleImage.alpha = 1;
                        articleTapZone.alpha = 1;
                        divider.alpha = .6;
                        [UIView commitAnimations];
                        [titleArray insertObject:articleTitle atIndex:0];
                        [bodyArray insertObject:articleBody atIndex:0];
                        [zoneArray insertObject:articleTapZone atIndex:0];
                        [imageArray insertObject:articleImage atIndex:0];
                        [hasImageArray insertObject:@"1" atIndex:0];
                        [stories addObject:[item copy]];
                        NSLog(@"Set up article: %@ With Image Type: %@ Position: 1", currentTitle, [currentImage substringFromIndex: [currentImage length] - 3]);
  8. MattInOz macrumors 68030


    Jan 19, 2006
    What happens if you comment out this line?
    edit: and just feed the raw summary to the textView.text.
    textView.text = [summary stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    Is this delivering any text to be displayed, or is the problem that you end up with no text to display?
    You might want to trying logging out textView.text to see what should be seen.

    Also I'm curious if there is reason you didn't use a UITableView?
    Not that a tableview would fix the problem at hand but sounds like it could make other things easier.
  9. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    That is what's delivering the text and subsequently if you remove that line there is no text in any of the views.

    I have tried logging the text and it all appears.

    I just found a way to fix it but it's not very pretty. When you swipe up to bring the next set of text views on screen I change the font size to 16.9 then back to 17 and then they all have text in them however it delays the move animation.
  10. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Few questions:

    Is it possible that there's one view on top of another?

    What happens if you use the standard system font? Or don't set the font at all just use the default?

    Do you set the text some other place in the code? Is it possible that you set the text while the animation is in progress?
  11. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    To question 1: The views are created from the frame of another view and then moved so they are overlapping shortly.

    Question 3: I don't set the text anywhere else.

    Question 2: That had an interesting effect, now most of them have there text in them however out of 20, 3 don't have content verses of 20 about 15 didn't before.
  12. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    When does this code run? viewDidLoad, viewWillAppear, in response to some user action? It's not running in a background thread is it?
  13. harryslotwiner thread starter macrumors newbie

    Mar 28, 2012
    The code is running once the JSON parser I have running finishes.

    So, not in the background.

Share This Page