views:

549

answers:

2

Hi Everyone:

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
+2  A: 

In the delegate method textField:shouldChangeCharactersInRange:replacementString: you should measure your UITextField's text size by using sizeWithFont:constrainedToSize: and use the returning CGSize's width parameter to set your UITextField's bounds width.

- (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
Hi luvieere: Thanks for your advice! Unfortunately, it is seeming to expand on both sides and seems to randomly cut off pieces of text on the left side. Is there some way to fix this problem?
PF1
Try setting its frame instead of its bounds and instead of `yourTextField.bounds.origin.x` put a fixed value. I'll test myself tomorrow, and if what I've told you doesn't work out I'll try some more.
luvieere
luvieere: Thanks again for your advice. However, setting it to the frame and changing it to a set value of the center point doesn't seem to help that much. It still has that odd problem of moving/disappearing when it gets too wide.
PF1
You're not supposed to set the center point, but the left edge, and the problem of getting too wide is solved by limiting its growth up to a certain level: `if (width <= K) yourTextField.bounds = CGRectMake(25, 50, width, yourTextField.bounds.size.height); //else do nothing`
luvieere
luvieere: Maybe it's another piece of code causing problems, but the same problem still occurs. The UITextField seems to float to the middle of the view and disappears when the text gets too long.
PF1
could you post more of your code so it will be easier to spot the error?
luvieere
I've posted the code for a sample UIView that demonstrates the error. Could a problem be caused when it gets the bounds information from the current text without the user's changes being applied?
PF1
I've solved the "disappearing" problem by adding lineBreakMode:UILineBreakModeHeadTruncation but still have the problem where it floats to the center of the view. Is there any way to fix this?
PF1
A: 

Why can't you just say:

if (yourTextField.frame.size.width > self.frame.size.width) {

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

}
Jaba
Hi Jaba: The problem with that code is that I need the text field width to have an original size, and then expand that width automatically as it grows. So the user can only edit the text if they click directly on it (and not on the extended frame).
PF1