ios - How to limit text length to fit width of dynamically created UITextField -


i have dynamically created uitextfields have different widths , font sizes. know how limit length of text in uitextfield, can fixed characters number. need dynamically limit characters number fit uitextfields. suppose every time new character typed should use cgsize , text length font size compare uitextfield width , limit characters number if exceeded uitextfield width. unfortunately not sure how start it. know code snippets me?

you can start code:

- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string {     nsstring *text = textfield.text;     text = [text stringbyreplacingcharactersinrange:range withstring:string];     cgsize textsize = [text sizewithfont:textfield.font];      return (textsize.width < textfield.bounds.size.width) ? yes : no; } 

after ios 7 changes sizewithfont sizewithattributes.

here code changes:

- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string {     nsstring *text = textfield.text;     text = [text stringbyreplacingcharactersinrange:range withstring:string];     cgsize textsize = [text sizewithattributes:@{nsfontattributename:textfield.font}];      return (textsize.width < textfield.bounds.size.width) ? yes : no; } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -