简体   繁体   中英

Adjusting the height of the UITextField with editing using iOS Autolayout

How do we increase the height of UITextField while editing using autolayout. I have a UIScrollView with four UITextField's . Currently all the textfield's are all of constant height. I need only a particular textfield to grow its height based on the text while editing.

You need to set height constrain for each text field.

And on edit did end method you need to identify the textfield by tag and than change the constant of the appropriate textfield.

for ex. heightTxt1,heightTxt2,heightTxt3,heightTxt4 are constraint.

Than change while edit did end called for textfield 1

   heightTxt1.constant= (count height on the basis of text and font style,size);

This should resolve your issue,

Step 1: Set no of lines in your label as 0

在此处输入图片说明

Step 2: Add this method in your class file

//-- Dynamic label frame depend on text 
-(CGRect)getLabelHeightForIndex:(UILabel *)label label_string:(NSString *)label_string
{
    CGSize maxSize = CGSizeMake(label.frame.size.width, 999);
    CGRect contentFrame;
    CGSize contentStringSize;
    NSString *version = [[UIDevice currentDevice] systemVersion];
    NSString *contentStr = label_string;

    if ([version floatValue] < 7.0)
    {
        contentStringSize = [contentStr sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];
    }
    else
    {
        NSDictionary *contentDic = [NSDictionary dictionaryWithObjectsAndKeys:label.font, NSFontAttributeName, nil];
        contentStringSize = [contentStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil].size;
    }
    contentFrame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, contentStringSize.height);
    return contentFrame;
}

Step 3: Access dynamic frame for label using following code

//-- Get dynamic label height from given string
    CGRect contentFrame = [self getLabelHeightForIndex:Label label_string:Label.text];
    Label.frame = contentFrame;
    CGFloat LabelHeight = contentFrame.size.height;

First off, you will want to store the height constraint for that specific UITextField in a property.

For example: @property (nonatomic, weak) IBOutlet NSLayoutConstraint *specificTextFieldHeightConstraint;

Then, implement -[UITextFieldDelegate textFieldDidBeginEditing:] inside your UIViewController subclass:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    specificTextFieldHeightConstraint.constant = expandedHeightGoesHere;
}

Finally, you probably want to return the height back to normal by implementing -[UITextFieldDelegate textFieldDidEndEditing:] inside your UIViewController subclass:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    specificTextFieldHeightConstraint.constant = originalHeightConstant;
}

If you set equal heights constraints across all of the UITextFields , you will need to set the priority of the equal height constraint that is attached to specificTextField to low when editing, and set it back to high when editing ends. You will still need a height constraint on specificTextField , but it should have a lower priority than the equal height constraint when specificTextField is not editing, and it should have a higher priority than the equal height constraint when specificTextField is editing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM