我在一个iOS
应用上,应该能够突出显示文字,并使其可以点击。
我在iOS中读到了NSAttributedString,但它比android中的Spannable更复杂。
是否有其他Objective c
方法可以做到这一点,如果没有;如何使用NSAttributedString
逐字突出显示段落,以及如何使我的文字可点击。
更新
我究竟想要的是每个单词应该可点击并且可以 在一个段落中突出显示为单个单词。
答案 0 :(得分:13)
我使用UITextView
找到了一个完美的解决方案,它会使UITextView
内的所有单词都可以点击。
首先,创建UITextView
并向其添加UITapGestureRecognizer
,如下所示:
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
上的任何触摸并获取被点击的单词:
-(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];
}
就是这样,希望这有助于其他人。
答案 1 :(得分:0)
不幸的是,您在寻找的行为并不像Spannable那样简单。
但是,它可以很容易地完成:
以下是一些代码字符串的示例代码,可帮助您入门:
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString: [NSString stringWithFormat:@"Text containing a bold word and another"];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14] range:NSMakeRange(18,4)];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14] range:NSMakeRange(32, 7)];
label.attributedText = string;