Im sur une application iOS
qui devrait pouvoir mettre en évidence le text, et le rendre cliquable aussi.
J'ai lu sur NSAtsortingbutedSsortingng dans iOS mais il est encore plus compliqué que Spannable dans android.
Y a-t-il un autre moyen d'atteindre cet Objective c
, sinon? que dois-je faire en utilisant NSAtsortingbutedSsortingng
pour mettre en surbrillance un paragraphe mot par mot, et comment rendre mon text cliquable.
Mettre à jour:
Qu'est-ce que je veux exactement que chaque mot devrait être cliquable et peut être mis en évidence comme un seul mot dans un paragraphe.
J'ai trouvé une solution parfaite en utilisant UITextView
, cela va rendre chaque mot de l' UITextView
cliquable.
Tout d'abord, créez un UITextView
et ajoutez un UITapGestureRecognizer
comme suit:
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 `NSMutableAtsortingbutedSsortingng` highlight the text ssortingng = [[NSMutableAtsortingbutedSsortingng alloc]initWithSsortingng:@"Any text to detect AB $ & - +"]; [ssortingng addAtsortingbute:NSFontAtsortingbuteName value:[UIFont systemFontOfSize:40.0] range:NSMakeRange(0, [ssortingng length])]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ; [paragraphStyle setAlignment:NSTextAlignmentCenter]; [ssortingng addAtsortingbute:NSParagraphStyleAtsortingbuteName value:paragraphStyle range:NSMakeRange(0, [ssortingng length])]; [textView setAtsortingbutedText:ssortingng]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)]; //modify this number to recognizer number of tap [singleTap setNumberOfTapsRequired:1]; [textView addGestureRecognizer:singleTap];
Ensuite, ajoutez le @selector
:
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer{ if(recognizer.state == UIGestureRecognizerStateRecognized) { CGPoint point = [recognizer locationInView:recognizer.view]; NSSsortingng * detectedText = [self getWordAtPosition:point inTextView: textView]; if (![detectedText isEqualToSsortingng:@""]) { NSLog(@"detectedText == %@", detectedText); } } }
Toute cette magie est liée à cette méthode, qui peut détecter n'importe quel contact sur l' UITextView
et get le mot tapé:
-(NSSsortingng*)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]; }
Et pour mettre en évidence le text:
-(void)setTextHighlited :(NSSsortingng *)txt{ for (NSSsortingng *word in [textView.atsortingbutedText componentsSeparatedBySsortingng:@" "]) { if ([word hasPrefix:txt]) { NSRange range=[self.textLabel.text rangeOfSsortingng:word]; [ssortingng addAtsortingbute:NSForegroundColorAtsortingbuteName value:[UIColor blueColor] range:range]; }} [textView setAtsortingbutedText:ssortingng]; }
Et c'est tout, j'espère que cela aidera les autres.
Malheureusement, le comportement que vous searchz n'est pas aussi simple dans Objective-C que Spannable.
Cependant, il peut être accompli assez facilement:
Voici un exemple de code pour la string atsortingbuée pour vous aider à démarrer:
NSMutableAtsortingbutedSsortingng *ssortingng = [[NSMutableAtsortingbutedSsortingng alloc] initWithSsortingng: [NSSsortingng ssortingngWithFormat:@"Text containing a bold word and another"]; [ssortingng addAtsortingbute:NSFontAtsortingbuteName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14] range:NSMakeRange(18,4)]; [ssortingng addAtsortingbute:NSFontAtsortingbuteName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14] range:NSMakeRange(32, 7)]; label.atsortingbutedText = ssortingng;