2016-02-08 58 views
0

試圖重寫drawPlaceholderInRect在Swift2.1讓我改變文字顏色,我有一個很難以下的OBJ-C轉換到斯威夫特:轉換對象 - 斯威夫特

(void)drawPlaceholderInRect:(CGRect)rect 
{ 
    UIColor *colour = [UIColor whiteColor]; 

    if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) { 

    NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font}; 
    CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil]; 
    [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; 
} 

我得到這個:

public override func drawPlaceholderInRect(rect: CGRect) { 
    let textDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] 

    ... 
    .. 
    . 
} 

有人可以幫忙嗎?

回答

0
func drawPlaceholder(in rect: CGRect) { 

    let colour: UIColor? = UIColor.white 

    if placeholder.responds(to: Selector("drawInRect:withAttributes:")) { 

     let attributes = [NSForegroundColorAttributeName: colour, NSFontAttributeName: font] 

     let boundingRect: CGRect = placeholder.boundingRect(with: rect.size, options: [], attributes: attributes, context: nil) 

     placeholder.draw(at: CGPoint(x: 0, y: (rect.size.height/2) - boundingRect.size.height/2), withAttributes: attributes) 
    } 
}