2013-09-30 16 views
12

Mam podklasy klasy UITextField i zrobił poniższy kodUITextField zastępczy ciąg jest zawsze Top wyrównane w ios7

- (void)drawPlaceholderInRect:(CGRect)rect 
{ 

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

} 

Napisałem również self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; linii kodu

Ten tekst zastępczy jest właściwie centrum wyrównane w ios6, ale nie w ios7, jest wyświetlane wyrównane do góry.

Mimo że tekst, który wpisuję, pojawia się w środku. Występuje tylko z łańcuchem zastępczym.

Próbowałem z Xib ustawić ciąg znaków zastępczych. W XIB jest wyświetlany prawidłowo, ale po uruchomieniu kodu pole zastępcze pola tekstowego jest wyrównane do góry.

Jakiekolwiek obejście tego problemu?

Odpowiedź Vadoffa zadziałała dla mnie. Nadal jest to pełna implementacja, która może pomóc każdemu z tym samym problemem.

drawInRect metoda jest przestarzałe w ios7 i drawInRectWithAttributes działa

- (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

Mam ten sam problem, nie ma jeszcze rozwiązania. – Vadoff

Odpowiedz

16

metody drawInRect wydaje się zachowywać się inaczej w iOS7, można spróbować dodać następujący wiersz i użyć jej jako rect rysować zamiast. Jest także kompatybilny wstecznie z pre-iOS7.

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

Jedna mała kwestia: zamiast używać rozmiaru czcionki, w wielu przypadkach liniowa wysokość zwróci lepsze wyniki dla rzeczy takich jak centrowanie, tekst wielowierszowy itd. Tak więc w moim kodzie używam self.font.lineHeight wszędzie używasz .pointSize – sujal

3

Kod poniżej działa na 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

Mam ustalone tego problemu przez instacji UITextFieldClass i zastąpić funkcję 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

Nieznaczna zmiana

- (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

Dlaczego nie po prostu przesunąć rect rysunek, a następnie wywołać super wołania metod realizacji? Kod Swift wynika ...

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

Ponieważ masz ustawione yourTextField.borderStyle = UITextBorderStyle ....

Powiązane problemy