【问题标题】:Add subtitle to center of pie-chart in swift快速将字幕添加到饼图的中心
【发布时间】:2017-01-01 04:33:02
【问题描述】:

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

下面是我的代码:

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
} 

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

【问题讨论】:

    标签: ios swift swift3 ios10 ios-charts


    【解决方案1】:

    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 之前添加了一个额外的空格以使其对齐,pieChart 有点挑剔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2020-10-06
      相关资源
      最近更新 更多