UITextField not scrolling horizontally

后端 未结 2 1825
醉梦人生
醉梦人生 2020-12-21 04:25

I am trying to make a small calculator app. When a UIButton is pressed, the Button title is added to a UITextField.

kind of:

myuitextfield.text = [my         


        
相关标签:
2条回答
  • 2020-12-21 05:05

    you have to add UITextview and limit the number of lines to 2.Textfield doesnt work with two lines.Textview is same as textfields except the delegates and some properties differ.

    0 讨论(0)
  • 2020-12-21 05:07

    If you are facing this issue on iOS7, I've managed to fix it after been inspired by this post. In my case I had a field for entering an email address and after reaching the edge, the user could carry on typing but the text would be invisible (off-field).

    First, add a callback to your UITextField so that you can track a text change to the field:

    [self.field addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    

    Then evaluate the size in pixels of the entered string as it is typed and change the text alignment from left to right when reaching the edge of the field area:

    - (void)textFieldDidChange:(NSNotification *)aNotif{
    
    float maxNumPixelsOnScreen = 235; // Change this value to fit your case
    CGSize maximumSize = CGSizeMake(maxNumPixelsOnScreen + 10, 1);
    NSString *aString = self.field.text;
    CGSize stringSize = [aString sizeWithFont:fieldFont
                               constrainedToSize:maximumSize
                                   lineBreakMode:NSLineBreakByWordWrapping];
    
    self.field.textAlignment = NSTextAlignmentLeft;
    if (stringSize.width >= maxNumPixelsOnScreen)
        self.field.textAlignment = NSTextAlignmentRight;
    }
    

    Note:

    • self.field is the offending UITextField
    • maximumSize: I'm adding 10 the the width to be slightly over the limit defined
    • fieldFont is the UIFont used to render the text field

    Hope it helps!

    0 讨论(0)
提交回复
热议问题