Mam niestandardową UICollectionViewCell z nie edytowalnym, nie do wybrania UITextView. W metodzie cellForItemAtIndexPath: ustawiam właściwość attributedText z UITextView, i wyświetla się dobrze ze wszystkimi atrybutami. Jednak gdy próbuję uzyskać dostęp do przypisanego tekstu później (w metodzie działania czytnika gestów dotykowych), wartość jest pusta.Przypisany tekst UITextView jest pusty po ustawieniu
Oto co mój kod wygląda następująco:
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;
}
Kolekcja widok komórkowy:
- (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);
}
}
która prowadzi do wyjścia po prostu "(null)". Jeśli jednak wydrukuję właściwość text UITextView, wydrukuje on tekst, ale nie tekst przypisany.
Czy robię coś nie tak? Naprawdę doceniam każdą pomoc
Dzięki!
Czy coś nie tak? Tak, nie uwzględniłeś kodu dla 'commentStringForComment'. –
Edytowane w celu uwzględnienia kodu dla tej metody. Nie uwzględniłem go pierwotnie, ponieważ już wiedziałem, że wartość zwracana była w porządku z uwagi na to, że przypisany ciąg znaków był wyświetlany poprawnie. – ryanthon