Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift round up for respondsToSelector() and sizeWithFont() #156

Closed
kutyel opened this issue Apr 14, 2015 · 1 comment
Closed

Swift round up for respondsToSelector() and sizeWithFont() #156

kutyel opened this issue Apr 14, 2015 · 1 comment

Comments

@kutyel
Copy link

kutyel commented Apr 14, 2015

I am translating into Swift the following Objective-C block code which belongs to JBChartInformationView.m (at the demo project):

- (void)layoutSubviews
{
    CGSize valueLabelSize = CGSizeZero;

    if ([self.valueLabel.text respondsToSelector:@selector(sizeWithAttributes:)])
    {
        valueLabelSize = [self.valueLabel.text sizeWithAttributes:@{NSFontAttributeName:self.valueLabel.font}];
    }
    else
    {
        valueLabelSize = [self.valueLabel.text sizeWithFont:self.valueLabel.font];
    }

    CGFloat xOffset = ceil((self.bounds.size.width - valueLabelSize.width) * 0.5);
    self.valueLabel.frame = CGRectMake(xOffset, ceil(self.bounds.size.height * 0.5) - ceil(valueLabelSize.height * 0.5), valueLabelSize.width, valueLabelSize.height);
}

This code basically loads dynamically an UILabel in the middle of the UIView respecting the relative font size and the padding of the view. This is the code I currently have:

override func layoutSubviews()
{
    var valueSize = CGSizeZero

    if valueLabel.text != nil {
        if ((self.valueLabel.text! as NSString).respondsToSelector(Selector("sizeWithAttributes:"))){
            valueSize = (self.valueLabel.text! as NSString).sizeWithAttributes([NSFontAttributeName:self.valueLabel.font])
        }
    }

   let xOffSet = ceil((self.bounds.size.width - valueSize.width) * 0.5)
   valueLabel.frame = CGRectMake(xOffSet, ceil(self.bounds.size.height * 0.5) - ceil(valueSize.height * 0.5), valueSize.width, valueSize.height)
}

This is not working correctly cause the label appears in the left-top corner of the view. I believe there is some round up in swift for not using the respondsToSelector() method but do not know how. I also want to avoid using sizeWithFont() cause it's _deprecated_.

Any help will be very welcomed!

@terryworona
Copy link
Collaborator

Hey @kutyel, you may find better luck asking on stack overflow (or similar).

I'm going to close this issue as it has no correlation to the core charting libraries.

FWW, I would start with inspecting the valueLabel and valueSize properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants