I have textView where user can write something and Label to display number of characters in textView. This is how i create my textView and label
var messageTextView: UITextView = {
let textView = UITextView()
return textView
}()
var messageTextViewTextCounterLabel: UILabel = {
let label = UILabel()
return label
}()
I have function to setup my label and uitextview.
let guide = self.view.safeAreaLayoutGuide
messageTextView.text = "Write something..:"
messageTextView.delegate = self
self.view.addSubview(messageTextView)
messageTextView.translatesAutoresizingMaskIntoConstraints = false
messageTextView.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 20).isActive = true
messageTextView.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -20).isActive = true
messageTextView.topAnchor.constraint(equalTo: guide.topAnchor, constant: 0).isActive = true
messageTextView.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: -20).isActive = true
messageTextViewTextCounterLabel.text = "0/800"
self.messageTextView.addSubview(messageTextViewTextCounterLabel)
messageTextViewTextCounterLabel.translatesAutoresizingMaskIntoConstraints = false
messageTextViewTextCounterLabel.trailingAnchor.constraint(equalTo: messageTextView.leadingAnchor, constant: 0).isActive = true
messageTextViewTextCounterLabel.bottomAnchor.constraint(equalTo: messageTextView.bottomAnchor, constant: 0).isActive = true
messageTextViewTextCounterLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
I position my label to be inside my TextView. I want it to be in bottom-right corner, but the label appears in bottom-right of the text in my textView. [
How can i place my label inside textview to be in bottom-right corner? (NOT bottom-right of the text)
messageTextViewTextCounterLabel.trailingAnchor.constraint(equalTo: messageTextView.leadingAnchor, constant: 0).isActive = true
It should be equal to messageTextView. trailingAnchor
messageTextView. trailingAnchor
.
import UIKit
class ViewController: UIViewController {
@IBOutlet var messageTextViewTextCounterLabel:UILabel!
@IBOutlet var messageTextField:UITextField!
@IBOutlet var messageTextView:UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional
self.messageTextField.delegate = self
self.messageTextView.delegate = self
}
}
UITextField
extension ViewController:UITextFieldDelegate {
//TextField Delegate Methods
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if(textField == messageTextField){
//Check TextField Lenth
let newLength = textField.text!.count + string.count - range.length
//Bind Lenth in label 8/800
messageTextViewTextCounterLabel.text = "\(newLength)/800"
return newLength <= 800
}
return true
}
//UITextView
extension ViewController:UITextViewDelegate {
//TextField Delegate Methods
func textViewShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(textView == messageTextView){
let newLength = textView.text!.count + text.count - range.length
messageTextViewTextCounterLabel.text = "\(newLength)/800"
return newLength <= 800
}
return true
}
You can solve this issue by giving the same trailing
and bottom
constraints to the label as well.
messageTextViewTextCounterLabel.trailingAnchor.constraint
(equalTo: guide.trailingAnchor, constant: -20).isActive = true
messageTextViewTextCounterLabel.bottomAnchor.constraint
(equalTo: guide.bottomAnchor, constant: -20).isActive = true
I changed
self.messageTextView.addSubview(messageTextViewTextCounterLabel)
to
self.view.addSubview(messageTextViewTextCounterLabel)
but there are also other solution. Instead of constraint equal to messageTextView we can change it to self.view.safeAreaLayoutGuide.trailingAnchor / bottomAnchor
@ilqarilyasov. solution:
messageTextViewTextCounterLabel.trailingAnchor.constraint
(equalTo: guide.trailingAnchor, constant: -20).isActive = true
messageTextViewTextCounterLabel.bottomAnchor.constraint
(equalTo: guide.bottomAnchor, constant: -20).isActive = true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.