Pavel Kaljunen
Pavel Kaljunen

Reputation: 1291

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:

enter image description here

how that can be done?

Upvotes: 1

Views: 1861

Answers (3)

hayesgm
hayesgm

Reputation: 9096

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)
    }
}

Upvotes: 0

Carles Estevadeordal
Carles Estevadeordal

Reputation: 1229

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];
    }
}

Upvotes: 1

skram
skram

Reputation: 5314

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.

Upvotes: 2

Related Questions