本文主要是介绍PPLable 过滤标点方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- (void)highlightWordContainingCharacterAtIndex:(CFIndex)charIndex {if (charIndex==NSNotFound) {//user did not click on any word[self removeHighlight];return;}NSString* string = self.label.text;
#pragma mark - 检测单词两侧的空格//compute the positions of space characters next to the charIndexNSRange end = [string rangeOfString:@" " options:0 range:NSMakeRange(charIndex, string.length - charIndex)];NSRange front = [string rangeOfString:@" " options:NSBackwardsSearch range:NSMakeRange(0, charIndex)];if (front.location == NSNotFound) {front.location = 0; //first word was selected}if (end.location == NSNotFound) {end.location = string.length-1; //last word was selected}NSRange wordRange = NSMakeRange(front.location, end.location-front.location);if (front.location!=0) { //fix trimmingwordRange.location += 1;wordRange.length -= 1;}
/****************************///获得单词NSString *word = [string substringWithRange:wordRange];NSLog(@"word %@",word);NSString *last = [word substringFromIndex:word.length - 1];NSString *first= [word substringToIndex:1];NSLog(@"last %@",last);//过滤标点符号if ([last isEqualToString:@","]|| [last isEqualToString:@"."]|| [last isEqualToString:@"?"]|| [last isEqualToString:@";"]|| [last isEqualToString:@"'"]|| [last isEqualToString:@"\""]){word = [word substringToIndex:word.length - 1];}if ([first isEqualToString:@","]|| [first isEqualToString:@"."]|| [first isEqualToString:@"?"]|| [first isEqualToString:@";"]|| [first isEqualToString:@"'"]|| [first isEqualToString:@"\""]){word = [word substringFromIndex:1];}word = [word lowercaseString];NSLog(@"word %@",word);
/****************************/if (wordRange.location == self.highlightedRange.location) {return; //this word is already highlighted}else {[self removeHighlight]; //remove highlight on previously selected word}self.highlightedRange = wordRange;//highlight selected wordNSMutableAttributedString* attributedString = [self.label.attributedText mutableCopy];[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:wordRange];self.label.attributedText = attributedString;
}
这篇关于PPLable 过滤标点方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!