快速将UILabel添加到UITextView

时间:2019-02-01 10:49:48

标签: ios swift xcode autolayout uitextview

我有textView,用户可以在其中写一些东西,还有Label以在textView中显示字符数。 这就是我创建textView和标签的方式

    var messageTextView: UITextView = {
    let textView = UITextView()
    return textView
}()
var messageTextViewTextCounterLabel: UILabel = {
    let label = UILabel()
    return label
}()

我具有设置标签和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

我将标签放置在TextView中。我希望它位于右下角,但标签显示在我的textView中文本的右下角。 [image1[1]

如何将标签放置在textview的右下角? (不是文本的右下角)

4 个答案:

答案 0 :(得分:0)

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

它应该等于messageTextView. trailingAnchor

答案 1 :(得分: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

}

答案 2 :(得分:0)

您也可以通过为标签赋予相同的trailingbottom约束来解决此问题。

    messageTextViewTextCounterLabel.trailingAnchor.constraint
    (equalTo: guide.trailingAnchor, constant: -20).isActive = true

    messageTextViewTextCounterLabel.bottomAnchor.constraint
    (equalTo: guide.bottomAnchor, constant: -20).isActive = true

enter image description here

答案 3 :(得分:0)

我改变了

 self.messageTextView.addSubview(messageTextViewTextCounterLabel)

 self.view.addSubview(messageTextViewTextCounterLabel)

但是还有其他解决方案。我们可以将其更改为self.view.safeAreaLayoutGuide.trailingAnchor / bottomAnchor

,而不是等于messageTextView的约束。

@ilqarilyasov。解决方案:

  messageTextViewTextCounterLabel.trailingAnchor.constraint
(equalTo: guide.trailingAnchor, constant: -20).isActive = true

messageTextViewTextCounterLabel.bottomAnchor.constraint
(equalTo: guide.bottomAnchor, constant: -20).isActive = true