gpt4 book ai didi

ios - 输入长文本时,textview 会附加与输入成比例的空格

转载 作者:行者123 更新时间:2023-11-28 08:04:20 25 4
gpt4 key购买 nike

the empty space on the end我正在获取我的 TextView 文本(我正在编写的 TextView )的大小,并将其设置为等于我正在显示它的 TextView 的大小。但是如果写更长的文本,当我发布它时,它会在我的 TextView 中显示更长的空白空间。我通过测量我正在写的 textV 的 content.height 为我的 TextView 的大小编写了代码。

} else if identifier == "save" {
print("Save button tapped")

if textViewWrite.text == "" {
print("text empty")
}
else{

print("\(textViewWrite.contentSize.height)")



let size: CGSize = textViewWrite.sizeThatFits(CGSize.init(width: textViewWrite.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
let insets: UIEdgeInsets = textViewWrite.textContainerInset;
let relevantHeight = size.height - insets.top - insets.bottom;

let text = Post(texts: "String", textHeight: relevantHeight, textWidth: textViewWrite.contentSize.width)
PostService.create(text: self.textViewWrite.text!, height: Int(textViewWrite.contentSize.height), width: Int(view.frame.width) )
print("\(textViewWrite.contentSize.height)")

// 1
let HomeViewController = segue.destination as! HomeViewController
// 2
HomeViewController.posts.append(text)

}
textViewWrite.text = ""
}
}

最佳答案

我以前看过您使用的方法,但是出于您的目的,我认为最好使用 layout constraints .在我看来,这要容易得多。

你想要做的是使用布局约束来约束接收 TextView 的高度属性。

在您的接收 TextView 中尝试像这样设置布局约束...

let captionContainer: UIView = {
let c = UIView()
c.translatesAutoresizingMaskIntoConstraints = false
c.backgroundColor = UIColor.lightGray
c.layer.cornerRadius = 6
return c
}()

let caption: UITextView = {
let c = UITextView()
c.textAlignment = .left
c.font = .systemFont(ofSize: 14)
c.isScrollEnabled = false
c.translatesAutoresizingMaskIntoConstraints = false
return c
}()

func setupCaption() {

view.addSubview(captionContainer)
view.addSubview(caption)

//Container Constratins
captionContainer.widthAnchor.constraint(equalToConstant: 250).isActive = true
captionContainer.heightAnchor.constraint(equalTo: caption.heightAnchor, multiplier: 1).isActive = true
captionContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
captionContainer.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

//Caption Constraints
caption.widthAnchor.constraint(equalToConstant: 250).isActive = true
caption.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
caption.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

// This constraint controls the maximum height the caption can have.
NSLayoutConstraint(item: caption, attribute: .height, relatedBy: .lessThanOrEqual, toItem: view, attribute: .height, multiplier: 0.25, constant: 0).isActive = true
}

然后将标题文本设置为您用于用户输入的任何文本字段或 TextView 。

祝你好运。

关于ios - 输入长文本时,textview 会附加与输入成比例的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45600553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com