大佬教程收集整理的这篇文章主要介绍了ios – 在UITextView中选择一个单词,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:labelText]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; CGRect bounds = label.bounds; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:bounds.size]; [layoutManager addTextContainer:textContainer];@H_607_29@ @H_944_2@现在你只需要找到被点击的字符的索引,这很简单!NSUInteger characterIndex = [layoutManager characterIndexForPoint:LOCATIOn intextContainer:textContainer fractionOfDistancebetweenInsertionPoints:NULL];@H_607_29@ @H_944_2@这使得找到这个词本身变得微不足道:if (characterIndex < textStorage.length) { [labelText.String enumerateSubStringsInRange:NsmakeRange(0,textStorage.length) options:NSStringEnumerationByWords usingBlock:^(NSString *subString,NSRange subStringRange,NSRange enclosingRange,BOOL *stop) { if (NSLOCATIOnInRange(characterIndex,enclosingRangE)) { // Do your thing with the word,at range 'enclosingRange' *stop = YES; } }]; }@H_607_29@ @H_944_2@原始答案,适用于iOS< 7 感谢@JP Hribovsek提供了一些有关此工作的提示,我设法为我的目的解决了这个问题.它感觉有点hacky,对于大型文本可能不会很好,但对于一次一段(这是我需要的),它很好. 我创建了一个简单的UILabel子类,允许我设置插入值:#import "WWLabel.h" #define WWLabelDefaulTinset 5 @implementation WWLabel @synthesize topInset,lefTinset,bottomInset,righTinset; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.topInset = WWLabelDefaulTinset; self.bottomInset = WWLabelDefaulTinset; self.righTinset = WWLabelDefaulTinset; self.lefTinset = WWLabelDefaulTinset; } return self; } - (void)drawTexTinRect:(CGRect)rect { UIEdgeInsets insets = {self.topInset,self.lefTinset,self.bottomInset,self.righTinset}; return [super drawTexTinRect:UIEdgeInsetsInsetRect(rect,insets)]; }@H_607_29@ @H_944_2@然后我创建了一个包含我的自定义标签的UIView子类,然后在标签上构建了标签中每个单词的文本大小,直到大小超过了点击位置的大小 – 这是被点击的单词.这不是完美的,但现在效果还不错. @H_944_2@然后我使用一个简单的NSAttributedString来突出显示文本:#import "WWPhoneticTextView.h" #import "WWLabel.h" #define WWPhoneticTextViewInset 5 #define WWPhoneticTextViewDefaultColor [UIColor blackColor] #define WWPhoneticTextViewHighLightcolor [UIColor yellowColor] #define UILabelMagicTopMargin 5 #define UILabelMagicLeftMargin -5 @implementation WWPhoneticTextView { WWLabel *label; NSMutableAttributedString *labelText; NSRange tappedRange; } // ... skipped init methods,very simple,just call through to configureView - (void)configureView { if(!label) { tappedRange.LOCATIOn = NsnotFound; tappedRange.length = 0; label = [[WWLabel alloc] initWithFrame:[self bounds]]; [label setLineBreakmode:NSLineBreakByWordWrapping]; [label setnumberOfLines:0]; [label setBACkgroundColor:[UIColor clearColor]]; [label setTopInset:WWPhoneticTextViewInset]; [label setLefTinset:WWPhoneticTextViewInset]; [label setBottomInset:WWPhoneticTextViewInset]; [label setrighTinset:WWPhoneticTextViewInset]; [self addSubview:label]; } // Setup tap handling UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@SELEctor(handleSingleTap:)]; singleFingerTap.numberOfTapsrequired = 1; [self addGestureRecognizer:singleFingerTap]; } - (void)setText:(NSString *)text { labelText = [[NSMutableAttributedString alloc] initWithString:text]; [label setAttributedText:labelText]; } - (void)handleSingleTap:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { // Get the LOCATIOn of the tap,and normalise for the text view (no margins) CGPoint tapPoint = [sender LOCATIOnInView:sender.view]; tapPoint.x = tapPoint.x - WWPhoneticTextViewInset - UILabelMagicLeftMargin; tapPoint.y = tapPoint.y - WWPhoneticTextViewInset - UILabelMagicTopMargin; // Iterate over each word,and check if the word contains the tap point in the correct line __block NSString *partialString = @""; __block NSString *lineString = @""; __block int currentLineHeight = label.font.pointSize; [label.text enumerateSubStringsInRange:NsmakeRange(0,[label.text length]) options:NSStringEnumerationByWords usingBlock:^(NSString* word,NSRange wordRange,BOOL* stop){ CGSize sizeForText = CGSizeMake(label.frame.size.width-2*WWPhoneticTextViewInset,label.frame.size.height-2*WWPhoneticTextViewInset); partialString = [NSString StringWithFormat:@"%@ %@",partialString,word]; // Find the size of the partial String,and stop if we've hit the word CGSize partialStringSize = [partialString sizeWithFont:label.font consTrainedToSize:sizeForText lineBreakmode:label.lineBreakmode]; if (partialStringSize.height > currentLineHeight) { // Text wrapped to new line currentLineHeight = partialStringSize.height; lineString = @""; } lineString = [NSString StringWithFormat:@"%@ %@",lineString,word]; CGSize lineStringSize = [lineString sizeWithFont:label.font consTrainedToSize:label.frame.size lineBreakmode:label.lineBreakmode]; lineStringSize.width = lineStringSize.width + WWPhoneticTextViewInset; if (tapPoint.x < lineStringSize.width && tapPoint.y > (partialStringSize.height-label.font.pointSizE) && tapPoint.y < partialStringSize.height) { NSLog(@"Tapped word %@",word); if (tappedRange.LOCATIOn != NsnotFound) { [labelText addAttribute:NSForegroundColorAttributENAME value:[UIColor blackColor] range:tappedRange]; } tappedRange = wordRange; [labelText addAttribute:NSForegroundColorAttributENAME value:[UIColor redColor] range:tappedRange]; [label setAttributedText:labelText]; *stop = YES; } }]; } }@H_607_29@
以上是大佬教程为你收集整理的ios – 在UITextView中选择一个单词全部内容,希望文章能够帮你解决ios – 在UITextView中选择一个单词所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。