在 swift 中将字幕添加到 pie-chart 的中心

Add subtitle to center of pie-chart in swift

我想在饼图中心的正文下方显示副标题,让用户知道数字代表什么。这可能吗?现在我有一个转换为字符串的数字。

下面是我的代码:

func setCenter(days: Int){

    let circleColor = UIColor.black
    var textColor = UIColor.white

    pieChart.holeRadiusPercent = 0.3
    pieChart.transparentCircleRadiusPercent = 0.0
    let dayString = String(describing: days)
    let centerText = NSAttributedString(string: dayString , attributes: [
        NSForegroundColorAttributeName:textColor,NSFontAttributeName: UIFont(name: "SubwayLogo",size:30)!])
    pieChart.centerAttributedText = centerText
    pieChart.centerTextRadiusPercent = 1.0
    pieChart.holeColor = circleColor
} 

如果您需要查看代码的其他部分,请告诉我。谢谢!

Nvm,我想通了。您必须创建 2 个可变属性字符串,然后将它们与第二个具有 "\n"

的字符串连接起来
func setCenter(days: Int){

    let circleColor = UIColor.black
    let textColor = UIColor.white

    pieChart.holeRadiusPercent = 0.3
    pieChart.transparentCircleRadiusPercent = 0.0
    let dayString = String(describing: days)
    let centerText = NSMutableAttributedString()
    let numberText = NSMutableAttributedString(string: " " + dayString, attributes: [NSForegroundColorAttributeName:textColor,NSFontAttributeName: UIFont(name: "SubwayLogo",size:30)!])
    let descriptionText = NSMutableAttributedString(string: "\n Days Left", attributes: [NSForegroundColorAttributeName:textColor,NSFontAttributeName: UIFont(name: "SubwayLogo",size:8)!])
    centerText.append(numberText)
    centerText.append(descriptionText)
    pieChart.centerAttributedText = centerText
    pieChart.centerTextRadiusPercent = 1.0
    pieChart.holeColor = circleColor
} 

确保尝试调整 2 个可变字符串的间距和大小。我在 dayString 之前添加了一个额外的 space 以使其对齐,饼图有点挑剔。