ryanthon
ryanthon

Reputation: 2634

UITextView attributed text is null after being set

I have a custom UICollectionViewCell with a non-editable, non-selectable UITextView. In the cellForItemAtIndexPath: method, I set the attributedText property of the UITextView, and it displays fine with all the attributes. However, when I try to access that attributed text later (in the action method of a tap gesture recognizer), the value is null.

Here is what my code looks like:

View Controller:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     TNCommentCollectionViewCell *commentCell = [collectionView dequeueReusableCellWithReuseIdentifier:kCommentCellIdentifier 
                                                                                          forIndexPath:indexPath];

     TNComment *comment = [self.comments objectAtIndex:indexPath.row];
     commentCell.textView.attributedText = [self commentStringForComment:comment];

     return commentCell;
}

- (NSAttributedString *)commentStringForComment:(DAFeedComment *)comment
{
    NSString *usernameString = [NSString stringWithFormat:@"@%@", comment.username];
    NSDictionary *usernameAttributes = @{ NSForegroundColorAttributeName : [UIColor usernameColor],
          NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f] };
    NSAttributedString *attributedUsernameString = [[NSAttributedString alloc] initWithString:usernameString attributes:usernameAttributes];
    NSMutableAttributedString *labelString = [attributedUsernameString mutableCopy];

    NSDictionary *commentAttributes = @{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f] };
    [labelString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [labelString appendAttributedString:[[NSAttributedString alloc] initWithString:comment.comment attributes:commentAttributes]];

    return labelString;
}

Collection view cell:

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.textView.textContainerInset = UIEdgeInsetsZero;

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewTapped:)];
    tapGesture.numberOfTapsRequired = 1;
    [self.textView addGestureRecognizer:tapGesture];
}

- (void)textViewTapped:(UITapGestureRecognizer *)recognizer
{
    UITextView *textView = (UITextView *)recognizer.view;

    NSLayoutManager *layoutManager = textView.layoutManager;
    CGPoint location = [recognizer locationInView:textView];
    location.x -= textView.textContainerInset.left;
    location.y -= textView.textContainerInset.top;

    NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
                                                      inTextContainer:textView.textContainer
                             fractionOfDistanceBetweenInsertionPoints:nil];

    if( characterIndex < textView.textStorage.length )
    {
        NSLog(@"%@", textView.attributedText);
    }
}

which leads to an output of just "(null)". If I print the text property of the UITextView however, it will print the actual text, just not the attributed text.

Am I doing something wrong? Would really appreciate any help

Thanks!

Upvotes: 8

Views: 1896

Answers (1)

Potajedehabichuelas
Potajedehabichuelas

Reputation: 575

I had the same issue. The solution for me was to enable the UITextView selectable property in the storyboard. After that it all worked.

Hope that helps

Upvotes: 14

Related Questions