UITextField not scrolling horizontally

一笑奈何 提交于 2019-12-18 08:52:00

问题


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 = [myuitextfield.text stringByAppendingString:[button currentTitle];

When I reach the end of my textfield, the text gets truncated. How can I disable this, so the textfield starts scrolling automatically and allows adding more characters?

I tried every possible option in Interface Builder, without any luck. Isn't the UITextField supposed to scroll automatically? I can see this behavior when a native keyboard is used and text is entered.

I have chosen UITextField, as I need only 1 Line.

To illustrate the Problem: When I enter text using my custom UIButtons text gets truncated

When I tap the UITextField and enter text using the keyboard I can enter unlimited text and the text is not truncated.


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/18119365/uitextfield-not-scrolling-horizontally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!