편집 할 수없고 선택할 수없는 UITextView가있는 사용자 지정 UICollectionViewCell이 있습니다. cellForItemAtIndexPath : 메서드에서 UITextView의 attributedText 속성을 설정하고 모든 특성을 표시합니다. 그러나 나중에 (탭 제스처 인식기의 동작 메서드에서) 해당 특성의 텍스트에 액세스하려고하면 값이 null입니다. 여기UITextView 속성이 설정되면 텍스트가 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;
}
Collection 뷰 세포 :
단지의 출력에 이르게- (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);
}
}
"(널)". 그러나 UITextView의 텍스트 속성을 인쇄하면 속성 텍스트가 아니라 실제 텍스트가 인쇄됩니다.
내가 잘못 했나요? 어떤 도움을 정말로 주겠 소.
고마워!
잘못된 것은 무엇입니까? 예, 당신은'commentStringForComment'에 대한 코드를 포함하지 않았습니다. –
해당 메소드에 대한 코드를 포함하도록 편집되었습니다. 왜냐하면 속성 문자열이 올바르게 표시 되었기 때문에 반환 값이 좋았다는 것을 이미 알고 있었기 때문에 원래 포함하지 않았습니다. – ryanthon