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:

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

I got this:

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

    ...
    ..
    .
}

Can someone help out please?

Answers


OmniProg February 2016

See if this is helpful:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216-CH2-ID0

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

Search




Leave an answer