简体   繁体   中英

Adding Subview over UITextField

I have a UITextView and I am trying to insert a popup subview on top of it when something specific has been entered. Pretty much an auto-complete (similar to native iOS autocorrection.

Using the delegate methods, I can find out how many characters the cursor is offset from the beginning. However, how do I find the precise location of the character/cursor? I need to be able to display the popup view at that specific location.

I tried calculating how wide is each character, but I think that this will not be consistent between Retina and non-Retina displays.

Any suggestions?

EDIT

I am not trying to implement a word auto-complete. I want to display a popup view of my own in order to display a tableview there with multiple options.

-(float)getWidthForString:(NSString*)str font:(UIFont*)font
 {
  CGSize boundingSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
  CGSize requiredSize = [str sizeWithFont:font constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];  
  return requiredSize.width;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM