I have a UITextField from which I'd like to determine the width of the entered text.
The bounds property has just the size of the Textfield but not of the text
If you have a UITextField or UITextView and want to stop users typing in more than a certain number of letters, you need to set yourself as the delegate for the control then implement either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).
An object that displays an editable text area in your interface.
CGFloat width = [aTextField.text sizeWithFont:aTextField.font].width;
Now that sizeWithFont:
is deprecated in iOS 7.0 use
CGFloat width = [tf.text sizeWithAttributes: @{NSFontAttributeName:tf.font}].width;
https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSString_UIKit_Additions/#//apple_ref/occ/instm/NSString/sizeWithAttributes:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With