2011-12-30 66 views

回答

9

創建的UITextField子類,並覆蓋drawPlaceholderInRect:

- (void) drawPlaceholderInRect:(CGRect)rect { 
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16.0]]; 
} 
+0

(void)drawPlaceholderInRect:(CGRect)rect {[self placeholder] drawInRect:rect withFont:[UIFont fontWithName:@「Helvetica」size:19.0]]; }放了這段代碼,但是它顯示了myviewcontroller可能不會響應t佔位符。而且字體也沒有改變 – stackiphone 2011-12-30 06:06:50

+0

@stackiphone您需要創建'UITextField'的子類並覆蓋該方法。 – fannheyward 2011-12-30 06:21:46

2

@fannheyward的回答是不錯的。但是,在iOS 7.0中已棄用– drawInRect:withFont:。在iOS的7,你應該使用:

- (void) drawPlaceholderInRect:(CGRect)rect { 
    UIFont *font = [UIFont fontWithName:@"STHeitiTC-Light" size:14.0];  
    [self.placeholder drawInRect:rect withAttributes:@{NSFontAttributeName: font}] 
} 

也請注意,「真正的」字體名稱可能是從一個它顯示在界面生成器不同。您可以使用下面的代碼在您的項目以獲取可用字體的「真實」姓名:

for (NSString *familyName in [UIFont familyNames]) { 
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) { 
     NSLog(@"%@", fontName); 
    } 
}