在Swift中将字幕添加到Pie-Chart的中心



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

以下是我的代码:

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
} 

确保与两个可变串的间距和大小相处。在几天之前,我添加了一个额外的空间以使其保持对齐,Piechart有点挑剔。

最新更新