2017-03-20 25 views
0

私はこのビューの上にカスタムビューを持っています。私はUILabelを持っています。ラベルはビューの中央に配置されます。ラベルには現在の速度が表示されます。整数部のラベルのテキストサイズは、小数部のテキストサイズよりも大きい。私はmonospacedDigitFontをラベルのテキストの両方の部分で使用して、数値が変わるときにテキストの揺れや移動を防ぎ、NSMutableAttributedStringがラベルのテキストの異なるサイズを設定できるようにします。明らかにそれは動作しません。iOS数字が変わるとUILabelのテキストが「揺れる」のを防ぐ

カスタムビュー:コードの

enter image description here

スニペット:

func updateSpeed(){ 
     dummySpeed += 4.0 

     speedometerView.currentSpeed = speedometerView.setSmoothSpeed(SpdAv: dummySpeed) 

     let myString = String(Float(round(speedometerView.currentSpeed * 10)/10)) 
     let attrString = NSMutableAttributedString(string: myString as String) 
     attrString.addAttribute(NSFontAttributeName, value: currentSpeedLabel.font.monospacedDigitFont, range: NSMakeRange(0, 1)) 
     attrString.addAttribute(NSFontAttributeName, value: currentSpeedLabel.font.monospacedDigitFont.withSize(20), range: NSMakeRange(2, 1)) 

     currentSpeedLabel.attributedText = attrString 
    } 

は私が間違って何をしますか?

答えて

0

あなたがiOSの初心者であれば、それは簡単ではありません。 ここに解決策があります:

@IBOutlet weak var currentSpeedLabel: UILabel! { 
     didSet{ 
      currentSpeedLabel.font = UIFont.monospacedDigitSystemFont(
       ofSize: UIFont.systemFontSize * 2, 
       weight: UIFontWeightRegular) 
     } 
    } 

func updateSpeed(){ 
     dummySpeed += 4.0 

     speedometerView.currentSpeed = speedometerView.setSmoothSpeed(SpdAv: dummySpeed) 

     let myString = String(Float(round(speedometerView.currentSpeed * 10)/10)) 
     let attrString = NSMutableAttributedString(string: myString as String) 
     attrString.addAttribute(NSFontAttributeName, value: UIFont.monospacedDigitSystemFont(
      ofSize: UIFont.systemFontSize, 
      weight: UIFontWeightRegular), range: NSMakeRange(attrString.length - 1, 1)) 

     currentSpeedLabel.attributedText = attrString 
    } 
関連する問題