2017-09-22 21 views
2

私はUIOutlineLabelというカスタムクラスを持っています。これは、ラベル内のテキストの周りにアウトラインを描画します。 Swift 4にアップデートしたので、次のエラーが発生します。
'[String:Any]'の値を期待される引数の型[NSAttributedStringKey:Any]に変換できません。XCode 9: '[String:Any]'型の値を期待される引数型 '[NSAttributedStringKey:Any]に変換できませんか?'

私はstrokeTextAttributesを変更しようとしている:

as! [NSAttributedStringKey : Any] 

が、これは、実行時エラーになります。

「UIOutlineLabel setOutlineWidthが廃止されており、スウィフト4で削除されます」のスウィフト言語ランタイムの警告もあります&「のUIOutlineLabel setOutlineColorが廃止されており、スウィフト4で削除されます」。

旧コード:

import UIKit 

class UIOutlineLabel: UILabel { 

    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) { 
     // Drawing code 
    } 
    */ 
    var outlineWidth: CGFloat = 1 
    var outlineColor: UIColor = UIColor.white 

    override func drawText(in rect: CGRect) { 

     let strokeTextAttributes = [ 
      NSAttributedStringKey.strokeColor.rawValue : outlineColor, 
      NSAttributedStringKey.strokeWidth : -1 * outlineWidth, 
     ] as! [String : Any] 

     self.attributedText = NSAttributedString(string: self.text ?? "", attributes: strokeTextAttributes) 
     super.drawText(in: rect) 
    } 
} 

答えて

2

私はあなたが使うべきだと思う:属性引数が、これはの実行時の警告に関するクラッシュが、何を解決し[NSAttributedStringKey : Any]?タイプ

+0

感謝を期待

override func drawText(in rect: CGRect) { let strokeTextAtrributes: [NSAttributedStringKey : Any] = [ NSAttributedStringKey.strokeColor : outlineColor, NSAttributedStringKey.strokeWidth : -1 * outlineWidth, ] self.attributedText = NSAttributedString(string: self.text ?? "", attributes: strokeTextAttributes) super.drawText(in: rect) } 

ので " UIOutLabel setOutlineWidthは廃止され、Swift 4 'で削除されます。'& 'UIOutlineLabel setOutlineColorは廃止され、Swift 4'では削除されます。 –

+0

デバッグコンソールには表示されません。 –

関連する問題