patreu22
patreu22

Reputation: 3365

How to change CenterText Font in PieChart in ios-charts?

I would like to change the Center Text Font and Font Size of a PieChart. I would like to make it as big as it's possible inside the center circle. Is there any on-board feature that helps me to change the font and font size or do i have to overwrite some Framework classes?

Didn't find any useful information in the documentation :(

That's my basic Chart Formating method:

    func setAttributes(view: PieChartView){
    view.legend.enabled = true
    view.descriptionText = ""
    view.userInteractionEnabled = false
    view.drawSliceTextEnabled = false
    view.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: ChartEasingOption.EaseInOutBack)
}

Regards!

Upvotes: 2

Views: 5101

Answers (3)

Energy
Energy

Reputation: 958

swift 5

  var pieChartView = PieChartView()
    ...

     let myAttrString = NSAttributedString(string: "My String", attributes: nil)
     pieChartView.centerAttributedText = myAttrString

Upvotes: 1

Milad Faridnia
Milad Faridnia

Reputation: 9477

Swift 3 You can use this code to change font in swift 3.

    let myAttribute = [ NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15.0)! ]
    let myAttrString = NSAttributedString(string: "My String", attributes: myAttribute)

    chart.centerAttributedText = myAttrString

Upvotes: 5

Wingzero
Wingzero

Reputation: 9754

The centered text in pie chart is called centerAttributedText, in PieChartView. It's NSAttributedText, so you can define many custom attributes.

You can simply change its font and size like below:

centerText = @"Whatever you like";

[centerText setAttributes:@{
                            NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f],
                            NSParagraphStyleAttributeName: paragraphStyle
                            } range:NSMakeRange(0, centerText.length)];

pieChartView.centerAttributedText = centerText;

It's in Objective-C, but should be easy for you to translate into swift, since you only need to care about the attributes

Upvotes: 3

Related Questions