限制UITextview的行数

83

我想知道如何在编辑UITextField时限制用户输入的行数(而不是像其他问题中询问的字符数)。

理想情况下,我要将输入限制为最多10行。

我需要从哪里开始? 我是否需要使用方法来完成这个功能?

 - (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView

你可以在这篇文章中找到答案:https://dev59.com/i0bRa4cB1Zd3GeqPxiNO。 - Zakaria
可能是Limiting text in a UITextView的重复问题。 - santhu
11个回答

0

Swift 5.7,iOS 16

在您的UIViewRepresentable包装器中,用于UITextView(或UITextField

func makeUIView(context: Context) -> UITextView {

    let textView = UITextView() // ..or MyCustomUITextView()

    // ..your existing makeUIView body here..

    // Limit to N = 3 visible (displayed) lines.
    textView.textContainer.maximumNumberOfLines = 3

    // When visible line count is exceeded typing can continue,
    // but what's entered will be truncated so hidden from view.
    textView.textContainer.lineBreakMode = .byTruncatingTail

    // ..or prevent further text entry when limit is hit.
    // See https://developer.apple.com/documentation/uikit/nslinebreakmode
    //textView.textContainer.lineBreakMode = .byClipping

    // Set delegate now that other changes are made.
    textView.delegate = self

    return textView
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接