将 UITextField 增加到一定长度
我有一个 UITextField,我想“自动”调整其边界大小,以便为字段中添加的字符串腾出空间。但是,我希望它的宽度达到一定程度的最大值。我可以采取的最佳方法是什么?
感谢您的任何帮助。
编辑:
测试视图.h
#import <UIKit/UIKit.h>
@interface TestingView : UIView <UITextFieldDelegate> {
}
@end
测试视图.m
#import "TestingView.h"
@implementation TestingView
- (void)awakeFromNib
{
CGRect testingBounds = self.bounds;
testingBounds.size.width = testingBounds.size.width - 20;
testingBounds.size.height = 30;
CGPoint testingCenter = self.center;
testingCenter.y = testingCenter.y - 75;
UITextField *testingField = [[UITextField alloc] initWithFrame:testingBounds];
testingField.center = testingCenter;
testingField.delegate = self;
testingField.placeholder = @"Testing";
[self addSubview:testingField];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int yourMaxWidth = 150;
float width = [textField.text sizeWithFont:
[UIFont systemFontOfSize: 14]
constrainedToSize:
CGSizeMake(yourMaxWidth, textField.bounds.size.height)].width;
textField.bounds = CGRectMake(textField.bounds.origin.x,
textField.bounds.origin.y,
width,
textField.bounds.size.height);
return YES;
}
@end
I have a UITextField that I would like to "automatically" adjust its bounds size in order to make space for the string added in the field. However, I would like it to max-out in terms of width at a certain amount. What is the best way that I can go about doing this?
Thanks for any help.
EDIT:
TestingView.h
#import <UIKit/UIKit.h>
@interface TestingView : UIView <UITextFieldDelegate> {
}
@end
TestingView.m
#import "TestingView.h"
@implementation TestingView
- (void)awakeFromNib
{
CGRect testingBounds = self.bounds;
testingBounds.size.width = testingBounds.size.width - 20;
testingBounds.size.height = 30;
CGPoint testingCenter = self.center;
testingCenter.y = testingCenter.y - 75;
UITextField *testingField = [[UITextField alloc] initWithFrame:testingBounds];
testingField.center = testingCenter;
testingField.delegate = self;
testingField.placeholder = @"Testing";
[self addSubview:testingField];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int yourMaxWidth = 150;
float width = [textField.text sizeWithFont:
[UIFont systemFontOfSize: 14]
constrainedToSize:
CGSizeMake(yourMaxWidth, textField.bounds.size.height)].width;
textField.bounds = CGRectMake(textField.bounds.origin.x,
textField.bounds.origin.y,
width,
textField.bounds.size.height);
return YES;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在委托方法中 textField:shouldChangeCharactersInRange:replacementString: 您应该使用 sizeWithFont:constrainedToSize: 并使用返回的
CGSize
的宽度参数来设置UITextField
的边界宽度。In the delegate method textField:shouldChangeCharactersInRange:replacementString: you should measure your
UITextField
's text size by using sizeWithFont:constrainedToSize: and use the returningCGSize
's width parameter to set yourUITextField
's bounds width.为什么你不能直接说:
Why can't you just say: