2017-01-01 33 views
0

我想在餅圖中心的主文本下方顯示一個字幕,以讓用戶知道該數字代表什麼。這可能嗎?現在我有一個我轉換成字符串的數字。將字幕添加到餅圖的中心

下面是我的代碼:

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 
} 

讓我知道如果你需要看代碼的其他部分。謝謝!

回答

0

嗯,我想通了。你必須創建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有點挑剔。

相關問題