UITextField不水平滚动 [英] UITextField not scrolling horizontally

查看:57
本文介绍了UITextField不水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小型计算器应用程序. 当按下UIButton时,Button标题将添加到UITextField.

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

种类:

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?

我尝试了Interface Builder中的所有可能选项,但没有任何运气. UITextField是否应该自动滚动?使用本机键盘并输入文本时,我可以看到此行为.

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.

我选择了UITextField,因为我只需要1行.

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

说明问题: 当我使用自定义UIButtons输入文本时,文本将被截断

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

当我点击UITextField并使用键盘输入文本时,我可以输入无限制的文本,并且文本不会被截断.

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

推荐答案

如果您在iOS7上遇到此问题,在受到

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

首先,将回调添加到UITextField中,以便您可以跟踪对该字段的文本更改:

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

注意:

  • self.field 是令人讨厌的UITextField
  • maximumSize:我将宽度加10,使其略微超出所定义的限制
  • fieldFont 是用于呈现文本字段的UIFont
  • 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

希望有帮助!

这篇关于UITextField不水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆