2013-09-24 35 views
21

我有我在哪裏執行下面的方法的UITextField子類:在子類中實現textRectForBounds和editingRectForBounds之後,UITextField不會滾動到最後?

- (CGRect)textRectForBounds:(CGRect)bounds { 

    return CGRectInset(bounds , 30, 7); 
} 

-(CGRect) editingRectForBounds:(CGRect)bounds { 
    return CGRectInset(bounds , 30, 7); 
} 

因爲這兩種方法中,UITextField不滾動到最後時輸入的文字變得比UITextField寬度更大。有沒有解決這個問題的方法?

+0

你實現這些創建文本週圍的插圖?還有其他方法可以實現這一點,也許更少車。 –

+0

你確定你不是壓倒一切嗎?因爲我用一個自定義文本實現了一個簡單的測試項目,只需重寫上面的兩個方法,文本水平滾動到最後。 – florian

+0

如果您將textfield直接放入xib中,則其工作正常,因爲adjustsFontSizeToFitWidth選項已設置爲「YES」。但如果你做動態分配的文本字段類,你應該設置textFieldObject.adjustsFontSizeToFitWidth = YES; – Yogendra

回答

4

這可能是您的自定義UITextFieldCALayer高度與插入和它的字體大小之間的衝突。

你可以試試這些調整的一個:

  • 減少字體大小;
  • 增加UITextField高度上Storyboard或編程在你的子類,如:

    - (void)drawRect:(CGRect)rect { 
        CGRect frameRect = self.frame; 
        frameRect.size.height = 35 //specify the height you want; 
        self.frame = frameRect; 
    } 
    
  • 減少對CGRectInset y座標值;

  • 或使用UIEdgeInsets可以更好地控制每個位置的值。喜歡的東西:

    - (CGRect)textRectForBounds:(CGRect)bounds{ 
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 30, 5, 30); 
        return UIEdgeInsetsInsetRect(bounds, contentInsets); 
    } 
    
    - (CGRect)editingRectForBounds:(CGRect)bounds{ 
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 30, 5, 30); 
        return UIEdgeInsetsInsetRect(bounds, contentInsets); 
    } 
    
2

原因 - 你正在使用固定字體大小的文本框的文本。 使用這個

textFieldObject.adjustsFontSizeToFitWidth = YES; 

簡單而簡單。如果你想設置您的最小字體大小,你可以使用這個

textFieldObject.minimumFontSize 

享受

0

我不知道這是你在尋找什麼,但修改editingRectForBounds下面的方式,將會使插圖動態的,所以一旦文本框得到「滿」,它會減少插入,所以文本將填充整個文本框。

假設textInset_是通緝插圖:

- (CGRect)editingRectForBounds:(CGRect)bounds { 
    CGFloat compensate = .0f; 
    CGSize size = [self.text sizeWithFont:self.font]; 
    CGFloat textboxWidth = CGRectGetWidth(self.width); 

    // If size of text plus inset (right side) is bigger 
    // than textbox's width, don't set an inset 
    if (size.width+textInset_ >= textboxWidth) { 
     compensate = 0; 
    } 

    // If text area (textbox width minus two insets) is less than 
    // text size, lessen the inset so the text fits in 
    else if (textboxWidth - textInset_*2 < size.width) { 
     compensate = fabs(size.width - (textboxWidth - textInset_)); 
    } 

    // Text fits in textbox WITH insets 
    else { 
     compensate = textInset_; 
    } 
    return CGRectInset(bounds, compensate, 10); 
} 
10

它不工作,只是因爲你已經在UITextField聲明的文本和您所申請的CGRectInset的字體大小。其實它應該是根據您設置的字體大小。例如,對於字體大小爲14.0,只需更改您的方法如下:

- (CGRect)textRectForBounds:(CGRect)bounds { 
    return CGRectInset(bounds , 30, 6); 
} 
-(CGRect) editingRectForBounds:(CGRect)bounds { 
    return CGRectInset(bounds , 30, 6); 
} 

原因是該文本的繪製矩形。例如,當我們將UITextField的字體大小設置爲14.0時,它至少需要18.0的高度才能調整幀中的文本。所以它需要在文本週圍增加4個像素,你可以說它覆蓋幀。由於在這種情況下,這種情況是不令人滿意的,因爲據我估計你已經調整了字體大小爲14.0和默認幀高度爲UITextField是30.0和你的邊緣掩碼是7.0每邊意味着總共14.0 。因此,對於文本的rect返回16.0,這反過來阻止操作系統更新文本框架,因爲它在圖形上下文之外。我希望你清楚。如果它解決了你的問題,那麼請接受我的答案。更好的回答如下:

- (CGRect)textRectForBounds:(CGRect)bounds { 
return CGRectInset(bounds , 30, (30 - self.font.pointSize - 4)/2); 
} 

-(CGRect) editingRectForBounds:(CGRect)bounds { 
    return CGRectInset(bounds , 30, (30 - self.font.pointSize - 4)/2); 
} 

享受!

+1

我在這裏看到的一個問題是,這個答案認爲' - [UIFont pointSize]'可以與像素距離互換。 – Sulthan

+0

這不是你投票的理由。檢查答案,然後進行投票。字體大小被認爲是計算特定字體大小的UITextField的最大邊緣蒙版。據我所知,像素轉換的字體大小完全取決於iOS是否是視網膜/平原。 –

+1

請不要編輯我的答案,使其毫無意義。這些數字是我寫的。 –

0
step 1 - make a custom text field MyTextField first 

@interface MyTextField : UITextField 
step 2 - override MyTextField methods as per your requirement 

///place holder position 

- (CGRect)placeholderRectForBounds:(CGRect)bounds 
{ 
    return CGRectInset(bounds, 8, 8); 
} 
// text position 
- (CGRect)textRectForBounds:(CGRect)bounds { 

    return CGRectInset(bounds, 8,4); 
} 

// text position while editing 
- (CGRect)editingRectForBounds:(CGRect)bounds { 

    return CGRectInset(bounds, 8, 4); 
} 
step 3- import MyTextField in your controller class & make object of MyTextField 

#import "MyTextField.h" 

-(void) viewWillAppear:(BOOL)animated { 

    MyTextField* textfield1 = [[MyTextField alloc] init]; 
    // and so on 

} 

參照此鏈接:

- (CGRect) textRectForBounds:(CGRect)bounds not being called

相關問題