Get X and Y coordinates of a word in UITextView
I asked a developer (TwoLivesLeft, the creators of Codea) how they did syntax highlighting in their app. He replied :
@TD2 the Codea editor is implemented using a UITextView. The
highlighting is done by overlaying subviews in the appropriate
positions — usually UILabels. They are dequeued from a re-use pool,
similar to the way UITableViewCells work. During scrolling, the lines
requiring re-highlighting pull markers out of the pool and lines that
have moved off screen dump their markers back into the pool.
Pushing an MFMailComposeViewController onto the navigation stack? Not presented modally How to be an Instagram sandbox user for any client in Instagram How to list out all MFI devies into the iOS app? Getting wrong carrier name in IOS Objective-C/Xcode 6: Best way to have a Search Bar populate a table view? Objects Sorting With date ,Time Problem in Array(Iphone Development)
Can anyone explain how I would get the x and y of a certain word?
- kCFStreamErrorDomainSSL, -9802 when connecting to a server by IP address through HTTPS in iOS 9
- Can i change multiplier property for NSLayoutConstraint?
- fatal error: unexpectedly found nil while unwrapping an Optional value in Swift
- kCVPixelFormatType_420YpCbCr8BiPlanarFullRange frame to UIImage conversion
- Xcode Version 6.1 (6A1030) - Apple Match O-Linker Error - Building
- UIScrollView contentSize not working
2 Solutions Collect From Internet About “Get X and Y coordinates of a word in UITextView”
UITextView conforms to UITextInput, of which a detailed description can be found here.
Take a look at the required methods “textRangeFromPosition:toPosition:”, “positionFromPosition:offset:”, “positionFromPosition:inDirection:offset:”, and some of the other geometric-based methods in the UITextInput Protocol. Those might provide the functionality you are looking for.
I have not actually tried to make sure these work the way you want them too, but that looks like its about what you need.
Let me know if you need any more help!
Here is some sample code of how to do this. I ended up getting the “firstRectForRange:” method to work. This code basically takes the last three letters of the UITextView “textStuff” and highlights it green.
UITextView *textStuff = [[UITextView alloc] init]; textStuff.frame = CGRectMake(2.0, 200.0, 200.0, 40.0); textStuff.text = @"how are you today?"; textStuff.textColor = [UIColor blackColor]; UITextPosition *Pos2 = [textStuff positionFromPosition: textStuff.endOfDocument offset: nil]; UITextPosition *Pos1 = [textStuff positionFromPosition: textStuff.endOfDocument offset: -3]; UITextRange *range = [textStuff textRangeFromPosition:Pos1 toPosition:Pos2]; CGRect result1 = [textStuff firstRectForRange:(UITextRange *)range ]; NSLog(@"%f, %f", result1.origin.x, result1.origin.y); UIView *view1 = [[UIView alloc] initWithFrame:result1]; view1.backgroundColor = [UIColor colorWithRed:0.2f green:0.5f blue:0.2f alpha:0.4f]; [textStuff addSubview:view1];
Result of running this code:
@bddicken’s https://stackoverflow.com/a/11487125/3549781 works like a charm. But the problem is, it doesn’t work on iOS 7+ if the text contains a newline “\n”. After a lot of searching I found a solution for that.
You have to ensure the layout of textView before calling firstRectForRange: by
Courtesy : UITextView firstRectForRange not working when there's new line characters in the mix
P.S : At first I added this as a comment to @bddicken’s answer. As most people don’t read comments I added this as an answer.
- Strange behaviour for recursive enum in Swift (Beta 7)
- Default optional parameter in Swift function
- How can I load a default language plist if the localized version does not exist for the current language?
- issue with CCScrollLayer in cocos2d-iphone-2.1-beta2
- How to remove a UIWindow?
- iOS 8: UINavigationController hide back button
- AVAudioRecorder record AAC/m4a
- How to set tcp_nodelay in GCDAsyncsocket?
- Invalid context when creating UIImageView
- Why does data returned from coredata become nil after tableview scroll in swift?
- Does the Facebook iOS SDK require the user to authenticate every time they use the app?
- Custom UITableViewCell does not show the labels in storyboard
- How to update NSTableView without using -reloadData?
- How to put a character limit on a UITextField
- CGPathRef from string