简体   繁体   中英

How to set limited width of text in uitextfield

I need to set limited width of text in my UITextField , because I put button on top of that UITextField

look at image:

在此输入图像描述

how that can be done?

You may need to subclass the UITextField and override editingRectForBounds: method. Try something like this..Of course adjust values accordingly.

- (CGRect)editingRectForBounds:(CGRect)bounds {
      return CGRectInset( bounds , 10 , 10 );
}

Accept this answer if it's the solution to your problem.

You can link the event editingChanged of the UITextView to the following method:

- (IBAction)textFieldChanged:(id)sender{
    if([sender.text length]>6){ 
        sender.text = [sender.text substringToIndex: 6];
    }
}

Just to add to this, you'll probably want to override both editingRectForBounds and textRectForBounds so that the text is correctly displayed when editing and not editing (see this post ). In Swift, this might be:

class MyUITextField: UITextField {

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, 18, 0)
    }

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, 18, 0)
    }
}

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