gpt4 book ai didi

ios - Objective-C中Android的 "Spannable"相当于什么

转载 作者:技术小花猫 更新时间:2023-10-29 11:17:57 27 4
gpt4 key购买 nike

我在 iOS 应用程序上应该能够突出显示文本,并使其也可以点击。

我读到了 NSAttributedString在 iOS 中,但它仍然比 Spannable 更复杂在安卓中。

是否有任何其他 Objective c 方法可以做到这一点,如果没有;我应该如何使用 NSAttributedString 逐字突出显示段落,以及如何使我的文本可点击。

更新:

What exactly i want that each word should be clickable and can be highlighted as a single word in one paragraph.

最佳答案

我找到了一个使用 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];

}

就是这样,希望这对其他人有帮助。

关于ios - Objective-C中Android的 "Spannable"相当于什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20044180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com