ios – 设置后,UITextView属性文本为null

我有一个自定义的UICollectionViewCell与一个不可编辑的,不可选择的UITextView.在cellForItemAtIndexPath:方法中,我设置了UITextView的attributedText属性,它显示了所有属性.
但是,当我稍后尝试访问该属性文本时(在点击手势识别器的操作方法中),该值为null.

这是我的代码的样子:

查看控制器:

- (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;
}

集合视图单元格:

- (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);
    }
}

这导致输出只是“(null)”.但是,如果我打印UITextView的text属性,它将打印实际文本,而不是属性文本.

难道我做错了什么?非常感谢任何帮助

谢谢!

解决方法

我遇到过同样的问题.我的解决方案是在故事板中启用UITextView可选属性.之后一切都奏效了.

希望有所帮助

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...