I want to insert a UITextField in between t开发者_Python百科he text of a UITextView to provide "fill in the blanks" functionality. I know that UITextField can't be added to the UITextView so I want to add UITextField to the super view of the UITextView by maintaining space for these UITextFields on the UITextView.
How can I determine the coordinates of a particular character with respect to the origin of the UITextView? The frame of the textView and its super view are the same. Can any one please tell me how to do this? Is there any other way to achieve fill in the blanks functionality with in a paragraph of text?
You may be able to use the UIStringDrawing additions to the NSString class to help find the location of characters.
// characterIndex is the index of the character for which you are finding the position
NSString *string = [myTextView.text substringToIndex:characterIndex];
CGSize sizeOfString = [string sizeWithFont:myTextView.font constrainedToSize:myTextView.contentSize];
If the character you are looking for is on the first line of the text view, sizeOfString
will give you the coordinates (offset by the line height).
If the character you are looking for is not on the first line, you would need to iteratively determine the location of each character up to the character you are looking for, modifying the substring to remove previous lines of text each time you detect the end of a line. You can detect the end of a line by noting when the sizeOfString
's height has changed between iterations.
精彩评论