Home Ask Login Register

Developers Planet

Your answer is one click away!

Polis February 2016

Convert Obj-C to Swift

Trying to override drawPlaceholderInRect in Swift2.1 so that I change the text color and I am having a hard time converting the following Obj-C to Swift:

    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];

I got this:

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


Can someone help out please?


OmniProg February 2016

See if this is helpful:


Also, if you have trouble translating Objective-C to Swift, which can be tricky sometimes, consider overriding the method in a derived Objective-C class and then using that class in Swift.

Post Status

Asked in February 2016
Viewed 2,059 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life