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

PieChart center Attributed Text #2441

Closed
laoSanDr opened this issue May 15, 2017 · 3 comments
Closed

PieChart center Attributed Text #2441

laoSanDr opened this issue May 15, 2017 · 3 comments

Comments

@laoSanDr
Copy link

To join in the string "\ n", how to make the second line words also centered

@liuxuan30
Copy link
Member

liuxuan30 commented May 16, 2017

Attributed Text should already support it, have you tried?

@laoSanDr
Copy link
Author

laoSanDr commented May 16, 2017

 NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.alignment = NSTextAlignmentCenter;

NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@", str, str2]];
    [centerText setAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:14.f],
                                NSParagraphStyleAttributeName: paragraphStyle
                                } range:NSMakeRange(0, str.length)];
    [centerText addAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:17.f],
                                NSForegroundColorAttributeName: UIColor.grayColor
                                } range:NSMakeRange(centerText.length - str2.length , str2.length)];
    chartView.centerAttributedText = centerText;

This is my code.The first line is centered, but the second line is on the left.

@liuxuan30
Copy link
Member

liuxuan30 commented May 17, 2017

I think this can be search or posted on Stack Overflow if you run into detail evils
But ChartsDemo pie chart center text works just fine:
image

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"Charts ABCDASDFEFG\nby Daniel"];
    [centerText setAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:13.f],
                                NSParagraphStyleAttributeName: paragraphStyle
                                } range:NSMakeRange(0, centerText.length)];
    [centerText addAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f],
                                NSForegroundColorAttributeName: UIColor.grayColor
                                } range:NSMakeRange(10, centerText.length - 10)];
    [centerText addAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:11.f],
                                NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]
                                } range:NSMakeRange(centerText.length - 19, 19)];
    chartView.centerAttributedText = centerText;

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