将UITextField增加到特定长度

3
我有一个UITextField,希望可以“自动”调整其边界大小以便为添加到该字段的字符串腾出空间。但是,我希望它在宽度上达到一定的最大值。我应该如何做才能实现呢?
谢谢任何帮助。
编辑:
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

你尝试过自动调整大小并在文本字段周围添加一些灵活的空间吗? - Jacob Relkin
2个回答

4
在委托方法textField:shouldChangeCharactersInRange:replacementString:中,您应该使用sizeWithFont:constrainedToSize:来测量UITextField的文本大小,并使用返回的CGSize的宽度参数来设置UITextField的边界宽度。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    float width = [yourTextField.text sizeWithFont:
           [UIFont systemFontOfSize: 14]  
                                      constrainedToSize:
           CGSizeMake(yourMaxWidth, yourTextField.bounds.size.height)].width;

    yourTextField.bounds = CGRectMake(yourTextField.bounds.origin.x,
                                      yourTextField.bounds.origin.y,
                                      width, 
                                      yourTextField.bounds.size.height);
}

嗨luvieere:感谢您的建议!不幸的是,它似乎在两侧扩展,并且似乎会随机切断左侧的文本片段。有没有什么办法解决这个问题? - PF1
尝试设置其框架而不是其边界,而且不要使用 yourTextField.bounds.origin.x,而是放置一个固定值。我明天会自己测试一下,如果我告诉你的方法行不通,我会再试试其他方法。 - luvieere
你不应该设置中心点,而是左边缘,并通过限制其增长到一定水平来解决过宽的问题:if (width <= K) yourTextField.bounds = CGRectMake(25, 50, width, yourTextField.bounds.size.height); //否则什么也不做 - luvieere
可能是其他代码出了问题,但同样的问题仍然存在。UITextField 似乎会漂浮到视图中间,并在文本过长时消失。 - PF1
我已经发布了一个示例UIView的代码,展示了这个错误。当它从当前文本获取边界信息而没有应用用户的更改时,是否会引发问题? - PF1
显示剩余3条评论

0
为什么你就不能说:“

”呢?
if (yourTextField.frame.size.width > self.frame.size.width) {

      yourtextField.frame.size.width = yourtextField.frame.size.width;

}

嗨Jaba:那段代码的问题在于,我需要文本字段宽度具有原始大小,然后随着文本的增长自动扩展该宽度。因此,用户只能在直接单击文本(而不是扩展框架)时编辑文本。 - PF1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接