但是,当我稍后尝试访问该属性文本时(在点击手势识别器的 *** 作方法中),该值为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 – 设置后,UITextView属性文本为null全部内容,希望文章能够帮你解决ios – 设置后,UITextView属性文本为null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)