How to determine the length of the text in a UITextfield

与世无争的帅哥 提交于 2019-12-03 10:41:30
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 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:CGSize = aTextField.attributedText.size()
let height = size.height
let width = size.width
HeikoG

textField is delegate which is attac in interface builder than......

[textField.text length]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!