2014-10-12 6 views
8

편집 할 수없고 선택할 수없는 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의 텍스트 속성을 인쇄하면 속성 텍스트가 아니라 실제 텍스트가 인쇄됩니다.

내가 잘못 했나요? 어떤 도움을 정말로 주겠 소.

고마워!

+0

잘못된 것은 무엇입니까? 예, 당신은'commentStringForComment'에 대한 코드를 포함하지 않았습니다. –

+0

해당 메소드에 대한 코드를 포함하도록 편집되었습니다. 왜냐하면 속성 문자열이 올바르게 표시 되었기 때문에 반환 값이 좋았다는 것을 이미 알고 있었기 때문에 원래 포함하지 않았습니다. – ryanthon

답변

14

나는 동일한 문제가있었습니다. 나를위한 해결책은 스토리 보드에서 UITextView 선택 가능 속성을 활성화하는 것이 었습니다. 그 후 그것은 모두 효과가있었습니다.

희망 하시겠습니까?

+2

YeahShad, 고마워요! 그것은 팅겨보고 작동했습니다. IB에서 Selectable로 필드를 표시했지만 런타임시 (ViewDidLoad에서) 필드의 Selectable 속성을 NO로 변경했습니다. 그래서 attributedText 데코 레이션을 얻지 만, 필드는 실행 중에 선택할 수없는 상태로 남아 있습니다. – jbbenni

+2

이것은 알려진 문제입니다. 그렇게 생각하시는 분들은 http://bugreport.apple.com에서 버그를 신고하고 버그 # 15854592를 참조하십시오. –

+0

애플이 우리에게 암기해야 할 많은 것들 중 하나는 애플이 표준 이하의 프로그래머를 고용하기 때문이다. 맹목적인 통보를받지 않으므로 낭비 할 시간이 더 있다면 버그 보고서를 제출하십시오. – amergin

관련 문제