tounaobun
tounaobun

Reputation: 14857

How to achieve this style of UITextField

I have a UITextField to let the user type 4 numbers which are returned from text message.I have set UITextField style and background image correctly but have no idea how to arrange these four numbers nicely.What I want is this: enter image description here

But the result is this:

enter image description here

Here is my setting for UITextField. enter image description here

How to separate the numbers nicely when typing from the keyboard,Any helps?

Upvotes: 1

Views: 42

Answers (2)

Vlad Papko
Vlad Papko

Reputation: 13312

I propose to use NSKernAttributeName attribute:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234"];
[attributedString addAttribute:NSKernAttributeName value:@(30) range:NSMakeRange(0, 4)];
self.textField.attributedText = attributedString;  

enter image description here

Upvotes: 1

Sergii Martynenko Jr
Sergii Martynenko Jr

Reputation: 1407

I would advice you place four text fields in one view, give it rounded corners and add it to your view hierarchy. Your view will have your desired background, and your text fields will be transparent (clearColor). Then, in your delegate, when in one textfield text entered, move your coursor to the next text field

Upvotes: 0

Related Questions