0

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. [image1[1]

How can i place my label inside textview to be in bottom-right corner? (NOT bottom-right of the text)

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
ShadeToD
  • 403
  • 9
  • 22
  • https://stackoverflow.com/questions/34922331/getting-and-setting-cursor-position-of-uitextfield-and-uitextview-in-swift – SPatel Feb 01 '19 at 11:51
  • try with `let endPosition: UITextPosition = textView.endOfDocument` – SPatel Feb 01 '19 at 11:53
  • nope , your solution doesn't work – ShadeToD Feb 01 '19 at 11:59
  • It not that easy – SPatel Feb 01 '19 at 12:05
  • @ShadeToD Should "messageTextViewTextCounterLabel" label's X position follow the text view's cursor position? Or X/Y position should be static? – RoHaN Feb 01 '19 at 13:13
  • 1
    Like [this](https://pasteboard.co/HZ9dV44.png)? – RoHaN Feb 01 '19 at 13:16
  • I want it to be static in bottom-right corner. I have already fixed it with adding label to it superview, but i still want to knows why i couldnt do this with adding label to TextView. – ShadeToD Feb 01 '19 at 13:19
  • @RoHaN, yes like this, but not outside TextView , but inside. – ShadeToD Feb 01 '19 at 13:19
  • If you add it inside the TextView wouldn't the label overlap the TextView's text, when maximum characters are added to TextView or when TextView's scroll view shows up. – RoHaN Feb 01 '19 at 13:50
  • @ShadeToD I'm talking about [this](https://pasteboard.co/HZ9vfgi.png) case. And [this](https://pasteboard.co/HZ9vIEC.png). – RoHaN Feb 01 '19 at 14:01

4 Answers4

0
messageTextViewTextCounterLabel.trailingAnchor.constraint(equalTo: messageTextView.leadingAnchor, constant: 0).isActive = true

It should be equal to messageTextView. trailingAnchor.

AIex H.
  • 21
  • 3
  • This doesnt help, label is still pinned to text inside textView – ShadeToD Feb 01 '19 at 11:30
  • 1
    Actually you can just add the label in text view’s superview insteads. And make the label align text view’s trailing and bottom. – AIex H. Feb 01 '19 at 11:38
  • I know that, but i wanted to know why i can't just add label to TextView. I changed TextView to TextField and it works fine , but i need TextView to support multiply lines of text. – ShadeToD Feb 01 '19 at 11:44
  • 2
    I think the problem is how you adding those constraints. If you create a subclass of UITextView, set and update constraints in updateConstraints then it should work – AIex H. Feb 01 '19 at 12:43
0
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

}
Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
0

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

enter image description here

iilyasov
  • 23
  • 1
  • 7
  • Sorry , but i have already tried this solution before. Its not working. – ShadeToD Feb 01 '19 at 14:44
  • 1
    It's going to work. I copied and pasted your code and only changed this part. Check your code. I've just added the screenshot - https://i.stack.imgur.com/xzJcG.png – iilyasov Feb 01 '19 at 14:52
  • Sorry i didint read whole code. I see u use self.view.safeAreaLayoutGuide and u are right it works , but u need to add minus constraint to position it in the way i want. I am still looking for answer if it possible to place it inside TextView with constraint to the textview – ShadeToD Feb 01 '19 at 15:05
0

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
ShadeToD
  • 403
  • 9
  • 22