2017-12-12 6 views
1

Sweft、Xdode、delegate、およびUIKitの理解を深めるためにmemeジェネレータアプリケーションを作成しようとしています。 NSAttributedStringKeyを使用して、黒い線で白にmemeテキストの色を設定しようとしています。黒いストロークは機能していますが、ユーザーがカメラのロールから画像を選択すると、前景色が適用されません。私はStackOverflowで検索しましたが、すべてのソリューションはSwift 4ではなくSwift 3に適用されます。どこが間違っていますか?Swift NSAttributedStringKeyがフォアグラウンドカラーを正しく適用しない

以下にソースコードを記載しました。

import UIKit 

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet weak var imagePickerView: UIImageView! 
    @IBOutlet weak var cameraButton: UIBarButtonItem! 
    @IBOutlet weak var topText: UITextField! 
    @IBOutlet weak var bottomText: UITextField! 

    let memeTextAttributes:[String:Any] = [ 
     NSAttributedStringKey.strokeColor.rawValue: UIColor.black, 
     NSAttributedStringKey.foregroundColor.rawValue: UIColor.white, 
     NSAttributedStringKey.font.rawValue: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!, 
     NSAttributedStringKey.strokeWidth.rawValue: 3.0] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Diable camer a button if camera ource isn't available 
     cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera) 
     topText.defaultTextAttributes = memeTextAttributes 
     bottomText.defaultTextAttributes = memeTextAttributes 
    } 

    // MARK: Delegate Methods 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      imagePickerView.image = image 
      self.dismiss(animated: true, completion: nil) 
     } 
    } 

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
     self.dismiss(animated: true, completion: nil) 
    } 

    @IBAction func pickAnImageFromAlbum(_ sender: Any) { 
     let pickerController = UIImagePickerController() 
     pickerController.delegate = self 
     pickerController.sourceType = .photoLibrary 
     present(pickerController, animated: true, completion: nil) 
    } 

    @IBAction func pickAnImageFromCamera(_ sender: Any) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = .camera 
     present(imagePicker, animated: true, completion: nil) 
    } 
} 
+1

これに応じてNSAttributedStringKey.someKeyに '.rawValue'を使用すべきではありません:https://stackoverflow.com/questions/46647438/swift-4-label-attributesまた、ストロークの幅はNEGATIVEでなければなりません: httraw://stackoverflow.com/questions/16047901/how-can-i-both-stroke-and-fill-with-nsattributedstring-w-uilabel – Larme

+0

Xcodeは、私が.rawValueを使用しないときに私に怒鳴ります。また、あなたが提供したリンクのアプローチを使用すると、これらの値をtextFieldに割り当てることができなくなりました。 –

+0

これは、どこで使われているのか分かりませんでしたが、 'memEtextAttributes'を使って' aUITextField.defaultTextAttributes'を設定しているので、大丈夫です。しかし、ストロークの幅が負でなければなりません。 – Larme

答えて

2

使用負strokeWithあなたにもforegroundColor設定します場合。それ以外の場合は、ストロークのみが表示されます。

let attributes = [ 
     NSAttributedStringKey.strokeColor.rawValue: UIColor.black, 
     NSAttributedStringKey.backgroundColor.rawValue: UIColor.red, 
     NSAttributedStringKey.foregroundColor.rawValue: UIColor.white, 
     NSAttributedStringKey.font.rawValue: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!, 
     NSAttributedStringKey.strokeWidth.rawValue: -4.5] 

編集:この場合、rawValueが必要な理由についてのrmaddyのコメントを参照してください。

+1

'UITextField defaultTextAttributes'は' [NSAttributedStringKey:Any] 'ではなく' [String:Any] 'を期待しているので、この場合は' rawValue'が必要です。 – rmaddy

+0

良い点。私はそれをUILabelでテストしたので、間違いを犯しました。それは編集されました。 –

関連する問題