UITextField cuts the text while editing
<p>I would like to ask you for help with this problem.
I got the UITextField which can take only 2 characters. When the application launches, there is a placeholder with YY characters. When user starts to edit and inserts number, everything is fine. After editing the textfield, first number is cuted,looks like shifted to the left, but when I delete both number and write them again, everything works even after repeated editing. </p>

<p>I tried so many options and even the log shows, that bounds width:20.000000 of textfield is higher then size of the text width:17.979492</p>

<p>Here's the code for calculating the size of the text</p>

<pre><code>-(CGFloat)lengthOfTextField:(NSString *)textField{

    CGSize textSize = [textField sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}];
    CGFloat width = textSize.width;

    return width;
}
</code></pre>

<p>Here's the code for setting the textfield frame</p>

<pre><code>-(void)setYearPosition
{
    if(self.yearField.text.length &gt;= 2){
    self.yearField.frame = CGRectMake(self.yearField.frame.origin.x,
                                      self.yearField.frame.origin.y,
                                      [self lengthOfTextField:self.yearField.text] + 10,
                                      self.yearField.frame.size.height);

    }
}
</code></pre>

<p>Thanks for your help.</p>