2015-09-04 91 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];