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

Center text vertically #682

Closed
hiworld75018 opened this issue Jan 18, 2016 · 9 comments
Closed

Center text vertically #682

hiworld75018 opened this issue Jan 18, 2016 · 9 comments

Comments

@hiworld75018
Copy link

When using big fonts, I noticed that text is not actually vertically centered.
Adding this in DrawUtils.drawText() makes it work:

if let font = attributes?[NSFontAttributeName] as? UIFont {
point.y -= font.lineHeight / 2
}

@liuxuan30
Copy link
Member

could you specify which font size are you using, also is it axis label text or value text? A screenshot is even better.

@hiworld75018
Copy link
Author

In case there is a single value, I use the chartView.noDataText field to display it, using font size 36 (with chartView.data == nil).

@liuxuan30
Copy link
Member

a single value, but using no data text? Confused. Can you post a screenshot?

@hiworld75018
Copy link
Author

You may try this code, to display the "single" value converted into a string that will sit in the middle of the "chart":

UIFontDescriptor* bodyDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFont* font = [UIFont fontWithDescriptor:bodyDescriptor size:36.]; // some BIG size

PieChartView* chartView = [[PieChartView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];

chartView.infoTextColor = [UIColor redColor];
chartView.infoFont = font;

chartView.noDataText = @"99%"; // the "single" value is converted into text here

// Remove the chart legend + label "description"
chartView.legend.enabled = NO;
chartView.descriptionText = @"";

@liuxuan30
Copy link
Member

if your "single value" means noDataText, on my side I see it like below:
The 99% seems a little bit lower than the center. Is this what are you trying to say?

pie

@liuxuan30
Copy link
Member

OK I checked the code:

    public class func drawText(context context: CGContext, text: String, var point: CGPoint, align: NSTextAlignment, attributes: [String : AnyObject]?)
    {
        if (align == .Center)
        {
            point.x -= text.sizeWithAttributes(attributes).width / 2.0
        }

It seems it just tried to center x but no y. I will file a PR later.

@hiworld75018
Copy link
Author

I think I've got a better way to fix it:

In ChartViewBase.drawRect(rect), use lineHeight to center vertically before calling ChartUtils.drawText()

CGContextSaveGState(context)
let lineHeight = infoFont.lineHeight
ChartUtils.drawText(..., point: CGPoint(x: frame.width / 2.0, y: frame.height / 2.0 - lineHeight / 2.0), ...)
if (noDataTextDescription != nil && (noDataTextDescription!).characters.count > 0)
{
...
ChartUtils.drawText(..., point: CGPoint(x: frame.width / 2.0, y: frame.height / 2.0 - lineHeight / 2.0 + textOffset), ...)
...
}

I noticed the same in PieChartRenderer.drawValues(context)

// y -= lineHeight
if (drawXVals && drawYVals)
{
y -= lineHeight
...
}
else if (drawXVals && !drawYVals)
{
y -= lineHeight / 2
...
}
else if (!drawXVals && drawYVals)
{
y -= lineHeight / 2
...
}

@liuxuan30
Copy link
Member

You could file a PR for this. Mine is closed since I don't see the big picture and don't have much time for details right now. Hope you can fix them all from my PR. There are two texts for no data case. We need adjust them all.

@danielgindi
Copy link
Collaborator

Modifying drawText is the wrong approach and will make all text drawing in axises incorrect.
Fix coming...

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

3 participants