UITextViews in a UITableView link detection bug in iOS 7

后端 未结 13 720
醉梦人生
醉梦人生 2020-12-01 07:19

I have custom UITableViewCells that contain a UITextView. I have link detection in the UITextView turned on in Interface Builder. When I first load the table view, everythin

相关标签:
13条回答
  • 2020-12-01 07:53

    Found a better way to solve this problem. This requires an extra step every single time you set text. But this definitely fixes the problem.

    _textView.selectable = NO; // set it to NO clears all possible data detection work so far.
    _textView.selectable = YES; // set it to YES so that actual links are detected.
    

    Basically data detection requires the selectable field to be set to YES to work. When you set it to NO, its completely removed.

    Note: This is only for ios7.

    0 讨论(0)
  • 2020-12-01 07:53

    Setting the text to nil did not work for me in a very similar problem, but setting scrollEnabled to NO, like suggested here, did the trick for me.

    Edit: In addition there was still a very special case, that caused problems: When a box began with a link and the new text was set to empty text (@"" - not nil!) the box somehow "broke" and from then on any new text became a link. My solution was to override setText to set [super text] to @"x" first and then to the actual new text. Setting it to nil instead did not solve this problem either.

    0 讨论(0)
  • 2020-12-01 07:55

    As any of the above solutions worked for me, a workaround I've found is to create your UITextView when you are supposed to update the text for each cell instead of reusing in the reusable cell.

    Your code in the UITableViewCellDataSource would be :

    - (void)updateDescriptionWithText:(NSString *)descriptionText
    {
        UITextView *descriptionTV = [[UITextView alloc] initWithFrame:aFrame];
        [descriptionTV setScrollEnabled:NO]; 
        [descriptionTV setEditable:NO]; 
        [descriptionTV setDataDetectorTypes:UIDataDetectorTypeLink]; 
        if ([descriptionV respondsToSelector:@selector(setSelectable:)]) 
         [descriptionTV  setSelectable:YES];
        [descriptionTV setText:descriptionText];
        [self addSubview:descriptionTV];
    }
    

    And in the UITableViewCell:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       ***your init code***
    
       [cell updateDescriptionWithText:description];
    }   
    
    0 讨论(0)
  • 2020-12-01 07:57

    This appears to be a bug in iOS 7.0's UITextViews. A similar question has a workaround which seems to help: set the text view's text to nil before setting it to the new text string.

    0 讨论(0)
  • 2020-12-01 08:01

    It seems that setting the text to nil before setting the new text doesn't work. You also might need to be able to scroll the text view, so setting the scrollEnabled might not be an option.

    However, it works when you create and set an attributed string to the cell's text view. I used a collection view, but it should be the same with table views. I wrote the following lines in the collectionView:cellForItemAtIndexPath:

    //get the color and other properties from your UITextView
    UIColor *textColor = cell.textView.textColor;
    UIFont *messageFont = cell.textView.font;
    
    //create your attributed string
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:yourStringForTheTextView attributes:@{NSForegroundColorAttributeName:textColor, NSFontAttributeName:messageFont}];
    
    //set it to your UITextView
    cell.textView.attributedText = attributedString;
    

    Hope this helps :).

    0 讨论(0)
  • 2020-12-01 08:03

    Non of the suggested workarounds work for me. So I decided to create a new UITextView and replace it with the old one, every time the cell is reused. It is not ideal for performance, but at least it works.

    0 讨论(0)
提交回复
热议问题