2015-09-04 92 views
7

我有一个UITextField子类,其中我重写了一些方法(请参阅下面的代码)。问题是,当我输入文本并且文本达到边界时,它将不再显示我输入的内容。在调试模式下,我发现UIFieldEditor比文本提交的要宽得多。UITextField文本不在左侧滚动

这里是UITextFiled子类代码:

- (instancetype)init 
{ 
    self = [super init]; 
    if (self) { 
     self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15); 
    } 
    return self; 
} 

- (id)initWithFrame:(CGRect)frame{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15); 
    } 
    return self; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:aDecoder]; 
    if(self){ 
     self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15); 
    } 
    return self; 
} 

- (CGRect)textRectForBounds:(CGRect)bounds { 
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; 
} 

- (CGRect)editingRectForBounds:(CGRect)bounds { 
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)]; 
} 

这里就是我使用这个类的代码:

// alloc/init 
     _myTextField.delegate = self; 
     _myTextField.backgroundColor = [UIColor whiteColor]; 
     _myTextField.layer.masksToBounds = YES; 
     _myTextField.layer.cornerRadius = 3.0; 
     _myTextField.returnKeyType = UIReturnKeyDone; 
     _myTextField.clearButtonMode = UITextFieldViewModeWhileEditing; 
     _myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
     _myTextField.autocorrectionType = UITextAutocorrectionTypeNo; 
     _myTextField.hidden = YES; 

     _tfButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
     [_tfButton setImage:[UIImage imageNamed:@"ic_edit"] forState:UIControlStateNormal]; 
     [self fs_addSubview:_tfButton]; 
     [_tfButton constrainWidth:22.]; 
     [_tfButton setTintColor:[UIColor greenColor]]; 
     [_tfButton constrainHeight:22.]; 
     [_tfButton constrainToRightOfView:_myTextField margin:-25.0]; 
     [_tfButton constrainEqualCenterYWithView:_myTextField offset:0.0]; 
     [_tfButton setUserInteractionEnabled:NO]; 


     UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)]; 

     _myTextField.leftView = leftView; 
     _myTextField.leftViewMode = UITextFieldViewModeAlways; 

如何获取文本左侧滚动出时用户在这个文本字段中输入一个大字符串?

回答

2

我面临同样的问题,但我没有任何UITextField子类。 问题是textfield在文本字段高度和字体大小之间有一定比例。将字体大小增加到特定值w.r.t以上的文本字段高度导致此问题。我所做的是将字体大小缩小一点,并且textfield再次开始工作。

+0

没有工作... – user426132

-1

有点着色,这是我试图重现你的代码后得到的,它对我来说看起来还不错。你能对预期的行为更具体吗?

enter image description here

6

你的代码是完全正确的。问题来自UITextField类。 当您在storyboard中放入UITextField,并将它的fontSize和minFontSize设置为50.此系统textField将与您的自定义textField具有相同的问题。

这只是因为UITextField不知道如何在狭窄的空间中绘制大字体。

我使用了textField的子类(默认高度为30),在将字体大小更改为7之后,一切正常。

+0

我永远不会有,虽然尝试改变我的手动设置字体大小,以解决这个问题。你是个天才。谢谢! – jp36

2

我有另一种解决方案。如果您在最小字体大小(在Storyboard上)勾选“调整为适合”选项,显然会起作用。不知何故,如果输入的文字多于右边距,文字会缩小。

或用代码:

self.textField.minimumFontSize = 12; 
self.textField.adjustsFontSizeToFitWidth = YES; 
0

减少从18到16的字体大小固定对我来说。不知道为什么,并不在乎:d

searchTF = [UITextField new]; 
searchTF.frame = CGRectMake(10, 10, w-140, 20); 
searchTF.backgroundColor = [UIColor redColor]; 
searchTF.font = [UIFont fontWithName:@"HelveticaNeue" size:18.0f]; 

searchTF.font = [UIFont fontWithName:@"HelveticaNeue" size:16.0f];