【发布时间】:2012-11-29 08:46:59
【问题描述】:
我的应用程序中有一个UITextField,宽度为 150 像素。当用户添加文本时,当文本大小超过textfield的宽度时,textfield的宽度应相应增加,使位置相同,所有输入的文本都能正常显示。我使用了以下代码,但没有运气。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
float width = [textField.text sizeWithFont:textField.font
constrainedToSize:CGSizeMake(300, textField.bounds.size.height)].width;
if (width > (textField.frame.size.width +50 )) {
CGRect rect = textField.frame;
rect.size.width +=10 ;
textField.frame =rect;
}
return YES ;
}
【问题讨论】:
-
嗨 prerna,你试过 [textView setContentHuggingPriority:NSLayoutPriorityFittingSizeCompression-1.0 forOrientation:NSLayoutConstraintOrientationVertical];
-
顺便说一句,它是文本字段。我试过了,但是我的应用程序崩溃了,异常无法识别的选择器发送到实例。
-
如果输入的文字很长怎么办?那么它应该是多行的,正确的naa?
-
不提供最大尺寸。
-
那你应该先设置最大宽度,然后让文本居中对齐,是不是很简单?
标签: objective-c ios cocoa-touch uitextfield