2

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!

patreu22
  • 2,988
  • 4
  • 22
  • 31

3 Answers3

5

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
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
3

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

Wingzero
  • 9,644
  • 10
  • 39
  • 80
1

swift 5

  var pieChartView = PieChartView()
    ...

     let myAttrString = NSAttributedString(string: "My String", attributes: nil)
     pieChartView.centerAttributedText = myAttrString
Energy
  • 940
  • 13
  • 20