菜鸟教程小白 发表于 2022-12-11 19:44:37

ios - 字段开始编辑时如何覆盖 UITextField 的呈现


                                            <p><p>我创建了一个 UITextField 的子类,其中有一个选择器作为输入 View 。我希望文本字段显示为两行,第一行显示时间,第二行显示日期。像这样:</p>

<p> <a href="/image/mSFh3.png" rel="noreferrer noopener nofollow"><img src="/image/mSFh3.png" alt="UITextView with two lines"/></a> </p>

<p>为此,我重写了 drawText 方法,如下所示:</p>

<pre><code>override func drawText(in rect: CGRect) {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .left
    paragraphStyle.lineBreakMode = .byWordWrapping
    let attrs = [NSFontAttributeName: UIFont.systemFont(ofSize: 12),
               NSParagraphStyleAttributeName: paragraphStyle,
               NSForegroundColorAttributeName: Utilities.COLOR_alarmText]
    (self.text! as NSString).draw(in: rect, withAttributes: attrs)
}
</code></pre>

<p>这很棒,我对此很满意。但是,当我点击该字段时,文本字段会使用不同的绘制方法重新呈现。所以它看起来像这样:</p>

<p> <a href="/image/Bkp5L.png" rel="noreferrer noopener nofollow"><img src="/image/Bkp5L.png" alt="UITextField when you tap to edit"/></a> </p>

<p>问题:有没有一种方法可以覆盖其他一些方法(我查看了 API 并没有找到任何东西)来保留我的两行字段?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>为了实现这一点,我建议使用 UIButton 而不是 UITextfield,因为您想从 piker 而不是键盘中进行选择,也不想编辑行为</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 字段开始编辑时如何覆盖 UITextField 的呈现,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44176340/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44176340/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 字段开始编辑时如何覆盖 UITextField 的呈现