相当于Android的“Spannable”是什么?在Objective-C中 [英] What is the equivalent of Android's "Spannable" in Objective-C
问题描述
我在一个 iOS
应用程序上应该能够突出显示文本,并使其可以点击。
Im on an iOS
app that should able to highlight text, and make it clickable too.
I了解 NSAttributedString 在iOS中,它仍然比android中的 Spannable 更复杂。
I read about NSAttributedString in iOS but it still more complicated than Spannable in android.
是否还有其他 Objective c
方法,如果没有;我应该怎么做才能使用 NSAttributedString
逐字突出显示段落,以及如何使我的文本可以点击。
Is there any other Objective c
way to do that, if not; what should i do using NSAttributedString
to highlight a paragraph word by word, and how to make my text clickable.
更新:
我想要的是每个单词应该可点击,并且可以是
在一个段落中突出显示为单个单词。
推荐答案
我找到了一个使用 UITextView
的完美解决方案,它会使 UITextView
中的每个单词都可点击。
I found a perfect solution using UITextView
, it will make every word in within the UITextView
clickable.
首先,创建 UITextView
并添加 UITapGestureRecognizer
到它如下:
Firstly, Create an UITextView
and add an UITapGestureRecognizer
to it as follows:
CGRect textViewFrame = CGRectMake(0, 40, 100, 100);
textView = [[UITextView alloc]initWithFrame: textViewFrame];
textView.textAlignment = NSTextAlignmentCenter;
textView.backgroundColor = [UIColor clearColor];
textView.editable = NO;
textView.selectable = NO;
[self.view addSubView:textView];
// i used to `NSMutableAttributedString` highlight the text
string = [[NSMutableAttributedString alloc]initWithString:@"Any text to detect A B $ & - +"];
[string addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:40.0]
range:NSMakeRange(0, [string length])];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
[textView setAttributedText:string];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
//modify this number to recognizer number of tap
[singleTap setNumberOfTapsRequired:1];
[textView addGestureRecognizer:singleTap];
然后添加 UITapGestureRecognizer
@selector
:
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer{
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [recognizer locationInView:recognizer.view];
NSString * detectedText = [self getWordAtPosition:point inTextView: textView];
if (![detectedText isEqualToString:@""]) {
NSLog(@"detectedText == %@", detectedText);
} }
}
所有这些魔法都与这个方法有关,女巫可以检测到 UITextView
上的任何触摸并获得被点击的单词:
All this magic is related to this method, witch can detect any touch on the UITextView
and get the tapped word:
-(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv
{
//eliminate scroll offset
pos.y += _tv.contentOffset.y;
//get location in text from textposition at point
UITextPosition *tapPos = [_tv closestPositionToPoint:pos];
//fetch the word at this position (or nil, if not available)
UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
return [_tv textInRange:wr];
}
并突出显示文字:
-(void)setTextHighlited :(NSString *)txt{
for (NSString *word in [textView.attributedText componentsSeparatedByString:@" "]) {
if ([word hasPrefix:txt]) {
NSRange range=[self.textLabel.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
}}
[textView setAttributedText:string];
}
就是这样,希望这有助于其他人。
And thats it, hope this helps others.
这篇关于相当于Android的“Spannable”是什么?在Objective-C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!