在以编程方式设置文本时,UITextField占位符在UITextField.text后面显示阴影。为什么?

问题描述 投票:0回答:1

我正在使用以下方法设置UITextField占位符的文本:

textField.placeholder = "placeholder"

然后,当我使用UIPickerView作为我的inputView时,当选择了一个选择器视图元素时,我通过执行placeholder并制作textField.placeholder = nil来更新textField.text = "pickerViewValue"。但占位符不清晰,文本后面有一个小阴影,如下所示:

https://imgur.com/a/sMHvEje

(你必须看得非常接近才能看到它。你会看到它在'黑暗L'后面隐约地说'尺寸'。

我已经尝试将文本和attributedText设置为占位符而不是使用占位符属性,但每当我这样做时,设置为text或attributedText的第一个值就像占位符文本一样保留在后台。我已经尝试将所有这些属性设置为nil和“”。没有骰子。想知道是否有人有类似的问题。

有格式化代码的麻烦所以这里是一个pastebin:

https://pastebin.com/pqmtaBeG

重要的是在ProductView的configure,updatePickerViews和extensions

let toolBar = UIToolbar()
  toolBar.barStyle = .default
  toolBar.isTranslucent = true
  toolBar.tintColor = globalAppleBlue
  toolBar.sizeToFit()

  let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)

  let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePickerViewButtonTapped(_:)))
  doneButton.setTitleTextAttributes([
    NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18)], for: .normal)

  toolBar.setItems([spaceButton, doneButton], animated: false)
  toolBar.isUserInteractionEnabled = true

  let textField = VoraTextField(leftPadding: globalPadding * 1.5, rightPadding: 0.0)
  textField.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 0.9)

  textField.placeholder = option.name.capitalized

  let imageView = UIImageView(image: UIImage(named: "right-angle"))
  imageView.contentMode = .scaleAspectFit

  textField.rightView = imageView
  textField.rightViewMode = .always
  textField.font = UIFont.boldSystemFont(ofSize: 16)
  textField.textColor = .black
  textField.delegate = self

  textField.inputAccessoryView = toolBar

  let pickerView = UIPickerView()
  self.pickerViewArray.append(pickerView)
  pickerView.showsSelectionIndicator = true
  pickerView.backgroundColor = .white
  textField.inputView = pickerView
  textField.borderStyle = .roundedRect

  pickerView.delegate = self
ios swift uitextfield uitextfielddelegate inputview
1个回答
0
投票

我不确定是什么导致了这个问题,但作为一种解决方法尝试在文本字段中添加纯色背景颜色。

UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1) // changed alpha to 1

当然,使用这些值,颜色会更暗。

© www.soinside.com 2019 - 2024. All rights reserved.