UITextView
임의의 태그와 텍스트로 생성 했는데 하나의 변수로 생성 UITextView
되었는데 생성 후 변수를 업데이트 할 수 있나요 (추가 버튼 클릭)? 임의의 숫자를 추가 할 수 있습니다 (예 : newText1, newText2 .. 등).
다음 UITextView
은 이미 새 변수로 생성 되었습니까?
추신 : 질문이 어리 석다면 최근에 Swift를 공부하기 시작했습니다.
@IBOutlet weak var addTextButton: UIButton!
@IBOutlet weak var StoriesView: UIView!
var newText = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addTextButton(_ sender: Any) {
let maxNumber = 10000
let i = Int(arc4random_uniform(UInt32(maxNumber)))
newText = UITextView(frame: CGRect(x: self.StoriesView.frame.origin.x + 40, y: self.StoriesView.frame.origin.y + 40, width: 380, height: 80))
self.StoriesView.addSubview(newText)
newText.font = UIFont(name: "Verdana", size: 11)
newText.text = "TAP TO EDIT #\(i)"
newText.sizeToFit()
newText.textColor = UIColor.black
newText.backgroundColor = UIColor.clear
newText.tag = i
newText.isEditable = true
newText.isSelectable = true
newText.isUserInteractionEnabled = true
newText.allowsEditingTextAttributes = true
newText.translatesAutoresizingMaskIntoConstraints = true
newText.enablesReturnKeyAutomatically = true
newText.delegate = self
}
UPD :
let fontToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
fontToolbar.barStyle = .default
fontToolbar.items = [
UIBarButtonItem(title: "Green", style: .plain, target: self, action: #selector(greenColor)),
UIBarButtonItem(title: "Blue", style: .plain, target: self, action: #selector(blueColor)),
UIBarButtonItem(title: "Red", style: .plain, target: self, action: #selector(redColor)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Close Keyboard", style: .plain, target: self, action: #selector(dismissKeyboard))]
fontToolbar.sizeToFit()
newText.inputAccessoryView = fontToolbar
키보드 위의 도구 모음에 버튼이 있습니다. 여기서 색상을 변경합니다.
@objc func redColor() {
newText.textColor = UIColor.red}
@objc func blueColor() {
newText.textColor = UIColor.blue}
@objc func greenColor() {
newText.textColor = UIColor.green}
그래서 색상은 새로 생성 된 UITextView
버튼을 클릭하면 새 texView를 생성하고 태그 값을 할당합니다. 추가되면 i 값을 +1로 업데이트하여 추가 된 모든 textView가 새 태그 값을 갖도록합니다.
var i = 1
var newText = UITextView()
@IBAction func addTextButton(_ sender: Any) {
newText = UITextView(frame: CGRect(x: self.StoriesView.frame.origin.x + 40, y: self.StoriesView.frame.origin.y + 40, width: 380, height: 80))
self.StoriesView.addSubview(newText)
newText.font = UIFont(name: "Verdana", size: 11)
newText.text = "TAP TO EDIT #\(i)"
newText.sizeToFit()
newText.textColor = UIColor.black
newText.backgroundColor = UIColor.clear
newText.tag = i
newText.isEditable = true
newText.isSelectable = true
newText.isUserInteractionEnabled = true
newText.allowsEditingTextAttributes = true
newText.translatesAutoresizingMaskIntoConstraints = true
newText.enablesReturnKeyAutomatically = true
newText.delegate = self
//increment i
i+=1
}
그런 다음 다음과 같은 태그 값을 통해 textField에 액세스 할 수 있습니다.
if let textView = self.StoriesView.viewWithTag(i) as? UITextView {
// textView.text = "change it"
}
최신 정보:
textView Delegate 메서드를 추가하고 textView가 편집을 시작하면 newText 값을 현재 편집중인 textView로 변경합니다.
class ViewController : UIViewController, UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
newText = textView
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다