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
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
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;
If you are looking for character count it is this
int textLength = [someTextField.text length]
If you are looking for pixel width try this
CGSize size = [someTextField.text sizeWithFont:someTextField.font];
//size.width contains the width
In Swift 3.0:
let size = aTextField.attributedText?.size()
let height = size.height
let width = size.width
CGSize size = [myTextField.text sizeWithFont:myTextField.font];
Documentation here: http://developer.apple.com/library/ios/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/sizeWithFont:
textField is delegate which is attac in interface builder than......
[textField.text length]