此代码适用于其他任何人,只有当我尝试在放置在Cell中的UITextView
上使用它时,我似乎无法得到属于tap的属性词。
UITapGesturRecognizer
添加在单元格本身上。
这是创建属性文本的代码:
//Color:
+ (NSAttributedString *)attributedMessageFromMessage:(NSString *)message {
NSArray* messageWords = [message componentsSeparatedByString: @" "];
NSMutableAttributedString *attributedMessage = [[NSMutableAttributedString alloc] initWithString:@""];
for (NSString *word in messageWords) {
NSDictionary * attributes;
if ([word isEqualToString:@""] || !word) {
return attributedMessage;
}
if([word characterAtIndex:0] == '@'){
attributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:72.0/255.0
green:192.0/255.0
blue:89.0/255.0
alpha:1.0],
wordType: userNameKey,
userNameKey:[word substringFromIndex:1],
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:18.0f]};
} else if([word characterAtIndex:0] == '#'){
attributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:16.0/255.0
green:96.0/255.0
blue:184.0/255.0
alpha:1.0],
wordType: hashTagKey,
hashTagKey:[word substringFromIndex:1],
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:18.0f]};
} else {
attributes = @{
NSForegroundColorAttributeName:[UIColor colorWithRed:145.0/255.0 green:145.0/255.0 blue:145.0/255.0 alpha:1], wordType: normalKey,
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:18.0f]};
}
NSAttributedString * subString = [[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@ ",word]
attributes:attributes];
[attributedMessage appendAttributedString:subString];
}
return attributedMessage;
}
这是在点击时调用的代码:
//Tap gestureRecornizer:
+ (WordObject *)messageTapped:(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;
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if (characterIndex < textView.textStorage.length) {
NSRange range;
id wordTypeToFind = [textView.attributedText attribute:@"WordType"
atIndex:characterIndex
effectiveRange:&range];
if([wordTypeToFind isEqualToString:userNameKey]){
NSString *userName = [textView.attributedText attribute:userNameKey
atIndex:characterIndex
effectiveRange:&range];
//Returning the word that was tappet:
WordObject *wordObject = [[WordObject alloc]initWithWord:userName andType:NAME andWhasThereAWordClick:YES];
return wordObject;
} else if([wordType isEqualToString:hashTagKey]){
// TODO: Segue to hashtag controller once it is in place.
NSString *userName = [textView.attributedText attribute:hashTagKey
atIndex:characterIndex
effectiveRange:&range];
// [self openViewControllerForUserName:userName];
WordObject *wordObject = [[WordObject alloc]initWithWord:userName andType:HASHTAG andWhasThereAWordClick:YES];
return wordObject;
}
}
return nil;
}
id&#34; wordTypeToFind&#34;刚刚回来了。
我想也许是因为UITextView
放在一个单元格上,我需要考虑一些以太值?
修改
我正在检查这个问题,这很奇怪。 我试图在点击后插入然后在线恢复归属文本,并且我没有了! 这是怎么回事?并且,当属性字符串附加到不在Cell上的UITextView时,会发生这种情况。
//This is called on tap:
NSAttributedString *final = [TextHandlerHelper attributedTextViewString:stringSeperated];
//Final has a value of @"bla bla @bla"
[self.labelHashTagsBottum setAttributedText:final];
//Text value is nil!
NSString *text = [self.labelHashTagsBottum.attributedText string];
答案 0 :(得分:0)
由于一些奇怪的原因,归因于文本,无论何时在单元格Outlet中使用它,它都会自行释放(即使它是一个字符串引用)。
我不知道这种情况到底发生了什么,但仍然可以看到属性文本,但值:
cell.textView.attributedText
将是nill。
让我们说,你有像Instagram这样的UITable,在单元格上有可点击的HashTag。 属性文本的值每次都会返回nill。
解决方案
我发现解决这个问题的唯一方法是将UITextView作为子视图添加到Cells ContentView。
像这样:
[self.textView removeFromSuperview];
self.textView = nil;
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(43, 310, 224, 51)];
[self.contentView addSubview:self.textView];
self.textView.editable = NO;
[self.textView setBackgroundColor:[UIColor clearColor]];
self.textView.scrollEnabled = NO;