2013-09-30 92 views
12

對準我有子類UITextField類,做了下面的代碼的UITextField佔位符串總是頂在ios7

- (void)drawPlaceholderInRect:(CGRect)rect 
{ 

    [self.placeHolderTextColor setFill]; 
    [self.placeholder drawInRect:rect 
         withFont:self.placeHolderFont 
        lineBreakMode:NSLineBreakByTruncatingTail 
         alignment:NSTextAlignmentLeft]; 

} 

我也寫 self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 行代碼

此佔位符文本正確中心對齊在ios6但不是在ios7它顯示頂部對齊。

雖然我輸入的文字出現居中,但只有佔位符字符串的問題。

我試着用xib設置佔位符字符串。在XIB中顯示正常,但是當我運行代碼時,textfield佔位符是頂部對齊的。

針對此問題的任何解決方法?

Vadoff的答案爲我工作。這仍然是完全實現,可以幫助任何人有同樣的問題。

drawInRect方法ios7被棄用,drawInRectWithAttributes工作

- (void)drawPlaceholderInRect:(CGRect)rect 
{ 

    [self.placeHolderTextColor setFill]; 

    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.placeHolderFont.pointSize)/2 - 2, rect.size.width, self.placeHolderFont.pointSize); 
    rect = placeholderRect; 

    if(iOS7) { 

     NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init]; 
     style.lineBreakMode = NSLineBreakByTruncatingTail; 
     style.alignment = self.placeHolderTextAlignment; 


     NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.placeHolderFont, NSFontAttributeName, self.placeHolderTextColor, NSForegroundColorAttributeName, nil]; 

     [self.placeholder drawInRect:rect withAttributes:attr]; 


    } 
    else { 
     [self.placeholder drawInRect:rect 
          withFont:self.placeHolderFont 
         lineBreakMode:NSLineBreakByTruncatingTail 
          alignment:self.placeHolderTextAlignment]; 
    } 

} 
+0

我有同樣的問題,沒有解決呢。 – Vadoff

回答

16

drawInRect方法似乎表現不同的iOS7,你可以嘗試添加以下行,並用其作爲矩形,而不是畫。它也與iOS7之前的版本兼容。

CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize); 
+9

一個小問題:在許多情況下,lineHeight不會使用字體的pointSize,而是在定位,多行文本等方面返回更好的結果。所以,在我的代碼中,我一直在使用self.font.lineHeight你使用.pointSize – sujal

3

代碼波紋管適用於iOS 5/6/7

@implementation PlaceholderTextField 

- (void)drawPlaceholderInRect:(CGRect)rect 
{ 
    // Placeholder text color, the same like default 
    UIColor *placeholderColor = [UIColor colorWithWhite:0.70 alpha:1]; 
    [placeholderColor setFill]; 

    // Get the size of placeholder text. We will use height to calculate frame Y position 
    CGSize size = [self.placeholder sizeWithFont:self.font]; 

    // Vertically centered frame 
    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height - size.height)/2, rect.size.width, size.height); 

    // Check if OS version is 7.0+ and draw placeholder a bit differently 
    if (IS_IOS7) { 

     NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 
     style.lineBreakMode = NSLineBreakByTruncatingTail; 
     style.alignment = self.textAlignment; 
     NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil]; 

     [self.placeholder drawInRect:placeholderRect withAttributes:attr]; 


    } else { 
     [self.placeholder drawInRect:placeholderRect 
          withFont:self.font 
         lineBreakMode:NSLineBreakByTruncatingTail 
          alignment:self.textAlignment]; 
    } 

} 

@end 
1

我有固定的這個問題通過繼承UITextFieldClass並覆蓋drawPlaceholderInRect功能。

- (void)drawPlaceholderInRect:(CGRect)rect 
{ 

    if(IS_IOS7) 
    { 
     [[self placeholder] drawInRect:CGRectMake(rect.origin.x, rect.origin.y+10, rect.size.width, rect.size.height) withFont:self.font]; 
    } 
    else { 

     [[self placeholder] drawInRect:rect withFont:self.font]; 
    } 
} 
0

輕微更新

- (void) drawPlaceholderInRect:(CGRect)rect { 
    if (self.useSmallPlaceholder) { 
     NSDictionary *attributes = @{ 
           NSForegroundColorAttributeName : kInputPlaceholderTextColor, 
           NSFontAttributeName : [UIFont fontWithName:kInputPlaceholderFontName size:kInputPlaceholderFontSize] 
           }; 

     //center vertically 
     CGSize textSize = [self.placeholder sizeWithAttributes:attributes]; 
     CGFloat hdif = rect.size.height - textSize.height; 
     hdif = MAX(0, hdif); 
     rect.origin.y += ceil(hdif/2.0); 

     [[self placeholder] drawInRect:rect withAttributes:attributes]; 
    } 
    else { 
     [super drawPlaceholderInRect:rect]; 
    } 
} 

http://www.veltema.jp/2014/09/15/Changing-UITextField-placeholder-font-and-color/

0

爲什麼不乾脆轉移繪製矩形,然後調用父類方法實現通話? SWIFT代碼隨之發生

override func drawPlaceholderInRect(rect: CGRect) { 
    var newRect = CGRectInset(rect, 0, 2) 
    newRect.origin.y += 2 
    super.drawPlaceholderInRect(newRect) 
} 
0

因爲你已經設置yourTextField.borderStyle = UITextBorderStyle ....