필드에 추가 된 문자열을위한 공간을 만들기 위해 범위 크기를 "자동으로"조정하고 싶은 UITextField가 있습니다. 그러나, 나는 그것을 특정 양의 너비의면에서 최대로하고 싶습니다. 이 일을 할 수있는 가장 좋은 방법은 무엇입니까?UITextField를 특정 길이로 늘림
도움 주셔서 감사합니다.
편집 :
TestingView.h
#import <UIKit/UIKit.h>
@interface TestingView : UIView <UITextFieldDelegate> {
}
@end
당신이 sizeWithFont:constrainedToSize:를 사용하여 UITextField
의 텍스트 크기를 측정해야 textField:shouldChangeCharactersInRange:replacementString: 위임 방법에 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
당신이 그것을 자동 크기 조정 및 텍스트 필드 주위에 약간의 융통성있는 공간을 추가하려고 했습니까? –