删除边框时,会修剪UITextFiled的内容 [英] Content of UITextFiled gets trimmed when removing the border

查看:68
本文介绍了删除边框时,会修剪UITextFiled的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的UITextField,我希望删除边框.因此,我尝试将其边框颜色更改为背景颜色(边框宽度为1),但是这样做时,在角落处看到了模糊的边框线.接下来,我在属性检查器中将边框样式设置为none.当我运行该应用程序时,边框消失了.但是,当在其中输入文本时,该文本将在左侧被裁剪,如图中所示.我尝试将填充视图添加到文本字段中,但未能解决问题.我该如何解决?

For my UITextField, I want the border to be removed. So, I tried changing its border color to the background color (with border width of 1) but doing so, faint border lines are seen at the corners. Next, I set the border style to none in the attribute inspector. When I run the app, the border is gone. However, when the text is entered in it, the text gets cropped in the left side as shown in the image. I tried adding padding view to the textfield but it did not fix the issue. How can I solve this?

文本字段后跟一个标签.由于我希望标签跟随文本字段的内容,因此我没有设置文本字段的宽度.如图所示.当我使用 leftViewMode 总是 添加填充视图时,该设计未呈现,并且我收到控制台消息:

The textfield is followed by a label. Since I want the label to follow the textfield content, I have not set the width of the textfield. This is shown in the image. When I add padding view with leftViewMode as always, the design in not rendered, and i get the console message:

-更改仅转换图层中的属性masksToBounds无效

根据@Surjeet链接的答案之一,我尝试将textField扩展为:

Following one of the answers from @Surjeet's link, I tried extending the textField as:

class CustomTextField: UITextField {

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return self.textRect(forBounds: bounds)
    }
}

但是问题仍然没有解决.

But still the problem is not solved.

推荐答案

text_field.setLeftPaddingPoints(10)

text_field.setRightPaddingPoints(10)


extension UITextField {

    func setLeftPaddingPoints(_ amount:CGFloat){
       let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
       self.leftView = paddingView
       self.leftViewMode = .always
    }
    func setRightPaddingPoints(_ amount:CGFloat) {
      let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
      self.rightView = paddingView
      self.rightViewMode = .always
    }
}

这篇关于删除边框时,会修剪UITextFiled的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆