Swift NSAttributedStringKey没有正确应用前景色

时间:2017-12-12 14:14:02

标签: ios swift nsattributedstringkey

我正在尝试创建一个meme生成器应用程序,以便更好地理解Swift,Xdode,代理和UIKit。我试图使用NSAttributedStringKey将meme文本的颜色设置为白色并使用黑色笔划。黑色笔划正在工作,但当用户从相机胶卷中选择图像时,不会应用前景色。我已经搜索了StackOverflow,但所有的解决方案都适用于Swift 3,而不是Swift 4.我哪里出错?

我在下面提供了我的源代码。

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)
    }
}

2 个答案:

答案 0 :(得分:11)

如果您还要设置initComplete () {       this.api (). columns (). header (). each ((el, i) => {           $ (el) .attr ('style', 'min-width: 160px;')       }); }, ,请使用否定strokeWidth。否则,只能看到中风。

foregroundColor

修改:请参阅rmaddy关于我们在下面这种情况下需要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] 的原因的评论。

答案 1 :(得分:3)

负责此行程的行程宽度,负行程值可以解决您的问题。

Swift 4.2

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