-1

我试图让我UITextView feedbackInput在单击内部时删除占位符文本,然后让用户UITextView以黑色字体颜色输入。此外,如果用户在 内部单击UITextView,但未键入任何内容,然后在外部单击,则应重新显示占位符文本。目前,我的代码正确加载占位符文本,但是当用户单击UITextView占位符文本内部时不会立即删除,用户只能输入UITextView浅灰色占位符文本字体颜色,而不是黑色。

AddMeal查看代码:

import SwiftUI

class AddMeal: UIViewController, UITextViewDelegate {
    @State var stars = -1
    var feedbackInput = UITextView(frame: CGRect(x: 20, y: 440, width: 372, height: 100))

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .red
        
        // Add Meal Title Label
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
        label.center = self.view.center
        label.center.x = self.view.center.x
        label.center.y = 75
        label.font = label.font.withSize(30);
        label.textAlignment = .center
        label.textColor = .white
        label.text = "Add Meal"
        self.view.addSubview(label)
        
        // Meal Title Label
        let food = UILabel(frame: CGRect(x: 22, y: 100, width: 200, height: 50))
        food.font = label.font.withSize(20);
        food.textColor = .white
        food.text = "Meal Title"
        self.view.addSubview(food)
        
        // Food Title Input Text Field
        let sampleTextField =  UITextField(frame: CGRect(x: 20, y: 150, width: 372, height: 40))
        sampleTextField.placeholder = "Enter the Name of the Food or Drink..."
        sampleTextField.font = UIFont.systemFont(ofSize: 15)
        sampleTextField.borderStyle = UITextField.BorderStyle.roundedRect
        sampleTextField.autocorrectionType = UITextAutocorrectionType.no
        sampleTextField.keyboardType = UIKeyboardType.default
        sampleTextField.returnKeyType = UIReturnKeyType.done
        sampleTextField.clearButtonMode = UITextField.ViewMode.whileEditing
        sampleTextField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
        self.view.addSubview(sampleTextField)
        
        // Meal Calories Label
        let mealCalories = UILabel(frame: CGRect(x: 22, y: 200, width: 200, height: 50))
        mealCalories.font = label.font.withSize(20);
        mealCalories.textColor = .white
        mealCalories.text = "Meal Calories"
        self.view.addSubview(mealCalories)
        
        // Calories Input Text Field
        let sampleTextField2 =  UITextField(frame: CGRect(x: 20, y: 250, width: 372, height: 40))
        sampleTextField2.placeholder = "Enter the # of Calories..."
        sampleTextField2.font = UIFont.systemFont(ofSize: 15)
        sampleTextField2.borderStyle = UITextField.BorderStyle.roundedRect
        sampleTextField2.autocorrectionType = UITextAutocorrectionType.no
        sampleTextField2.keyboardType = UIKeyboardType.default
        sampleTextField2.returnKeyType = UIReturnKeyType.done
        sampleTextField2.clearButtonMode = UITextField.ViewMode.whileEditing
        sampleTextField2.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
        self.view.addSubview(sampleTextField2)
        
        // Meal Rating Label
        let mealRating = UILabel(frame: CGRect(x: 22, y: 300, width: 200, height: 50))
        mealRating.font = label.font.withSize(20);
        mealRating.textColor = .white
        mealRating.text = "Meal Rating"
        self.view.addSubview(mealRating)
        
        // Rating Stars
        let stars = UIHostingController(rootView: StarsView())
        self.view.addSubview(stars.view)
        self.addChild(stars)
        stars.view.backgroundColor = .clear
        stars.view.translatesAutoresizingMaskIntoConstraints = false
        stars.view.sizeToFit()
        stars.view.topAnchor.constraint(equalTo: mealRating.bottomAnchor, constant: 5).isActive = true
        stars.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        
        // Meal Feedback Label
        let mealFeedback = UILabel(frame: CGRect(x: 22, y: 390, width: 200, height: 50))
        mealFeedback.font = label.font.withSize(20);
        mealFeedback.textColor = .white
        mealFeedback.text = "Meal Feedback"
        self.view.addSubview(mealFeedback)
        
        // Feedback Input Field
        feedbackInput.delegate = self
        feedbackInput.text = "Enter Your Feedback on the Meal..."
        feedbackInput.layer.cornerRadius = 5
        feedbackInput.font = UIFont.systemFont(ofSize: 15)
        feedbackInput.textColor = .lightGray
        self.view.addSubview(feedbackInput)
    }
}

func textViewDidBeginEditing(textView: UITextView) {
    if textView.textColor == .lightGray {
        textView.text = ""
        textView.textColor = .black
    }
}

func textViewDidEndEditing(textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = "Enter Your Feedback on the Meal..."
        textView.textColor = .lightGray
    }
}

struct StarsView: View {
    @State var stars = -1
    var body: some View {
        HStack {
            ForEach(0..<5){ i in
                Image(systemName: "star.fill")
                    .resizable().frame(width: 30, height:30)
                    .foregroundColor(self.stars >= i ? .yellow : .gray)
                    .onTapGesture {
                        self.stars = i
                    }
            }
        }
    }
}

电流输出图像:

输出

如上所述,我怎样才能解决问题textViewDidBeginEditing并且textViewDidEndEditing没有被正确调用以使我正常工作?UITextView feedbackInput

4

2 回答 2

0

你需要设置placeholder,而不是text

override func viewDidLoad() {
    super.viewDidLoad()
 
    ...

    mealFeedback.placeholder = "Meal Feedback"
    feedbackInput.placeholder = "Enter Your Feedback on the Meal..."
}
func textViewDidBeginEditing(textView: UITextView) {
    if textView.textColor == .lightGray {
        textView.placeholder = ""
        textView.textColor = .black
    }
}

func textViewDidEndEditing(textView: UITextView) {
    if textView.text.isEmpty {
        textView.placeholder = "Enter Your Feedback on the Meal..."
        textView.textColor = .lightGray
    }
}
于 2020-12-29T20:03:39.433 回答
0

您的委托回调您的视图控制器上下文之外,但必须在内部

class AddMeal: UIViewController, UITextViewDelegate {
    // @State var stars = -1 // << !! BTW, don't use state here - it will not work

// ... other code

  func textViewDidBeginEditing(textView: UITextView) {
    if textView.textColor == .lightGray {
        textView.text = ""
        textView.textColor = .black
    }
  }

  func textViewDidEndEditing(textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = "Enter Your Feedback on the Meal..."
        textView.textColor = .lightGray
    }
  }
}
于 2020-12-30T05:18:38.937 回答