在UITextField中使用自定义字体会导致其在访问时略微偏移-是否已解决?

| 我在UITextField中有一个自定义字体,并且我已经注意到,当它被访问时(出现键盘时),文本向下移动的量非常小-可能是一个像素或两个或三个。 (当然,我没有办法对其进行测量,但是足以让我注意到。)然后,在关闭键盘后,它会再次向上移动。 我已经尝试在编辑时清除字段(这隐藏了初始下移的问题),但尚未解决问题。我也查看了IB中的各种属性,并尝试更改了一些属性,但问题仍然存在。在模拟器和iPad2上,我得到相同的结果。 (该字段距离键盘很远,因此,并不是整个视图移开-而是该特定文本字段的内容。) 我确定这是导致问题的自定义字体-没有它就不会发生。 任何想法如何解决这个问题?我当时想我可能需要以编程方式创建文本字段,而不是在IB中创建文本字段-我知道我可能应该在问这个问题之前先尝试一下,但是我不愿意在可能的情况下解决所有麻烦解决问题。 任何建议表示赞赏!     
已邀请:
        我也有这个问题。 要修复此问题,请子集ѭ0并实现以下方法以在不进行编辑和编辑时调整文本的位置。
- (CGRect)textRectForBounds:(CGRect)bounds {

    return CGRectInset( bounds , 8 , 8 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds {

    return CGRectInset( bounds , 8 , 5 );
}
    
        我的解决方案与McDJ的解决方案相同,但略有不同。子类化UITextField并仅覆盖以下内容:
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
  return CGRectOffset( [self editingRectForBounds:bounds], 0, 2 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
  return CGRectOffset( [super editingRectForBounds:bounds], 0, -2 );
}
使用我正在使用的自定义字体,正确的垂直调整是2点,这有助于占位符,“静态”和“编辑”文本都保持在同一垂直线上。     
        不幸的是,没有答案对我有用。 @blackjacx答案有效,但仅有时:( 我开始调试,这是我发现的内容: 1-真正的问题似乎是在类型为
UIFieldEditorContentView
的UITextField的私有子视图中 在下面您可以看到它的子视图的
y
UITextField
本身不同: 在意识到这一点之后,我想到了以下解决方法:
override func layoutSubviews() {
    super.layoutSubviews()
    fixMisplacedEditorContentView()
}

func fixMisplacedEditorContentView() {
    if #available(iOS 10, *) {
        for view in subviews {
            if view.bounds.origin.y < 0 {
                view.bounds.origin = CGPoint(x: view.bounds.origin.x, y: 0)
            }
        }
    }
}
您将需要子类化“ 0”并覆盖“ 8”,以增加将任何手动设置为负值的子视图中的“ 4”设置为“ 9”的功能。由于iOS 9不会发生此问题,因此我们在下面添加了一项检查,以仅在iOS 10上解决此问题。 您可以在下面看到结果: 2-如果用户选择选择文本的子范围,则此解决方法将不起作用(selectAll可以正常工作) 由于文本的选择对于我的应用程序不是必须的,因此我宁愿禁用它。为此,您可以使用以下代码(Swift 3):
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if #available(iOS 10, *) {
        if action == #selector(UIResponderStandardEditActions.select(_:)) {
            return false
        }
    }

    return super.canPerformAction(action, withSender: sender)
}
    
        适用于所有字体大小,不会导致与
clearButton
对齐。 子类“ 0”,并按如下所示重写它们:
- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    return CGRectOffset( bounds, 0, 4 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    return CGRectOffset( bounds, 0, 2);
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    return CGRectOffset( bounds , 0 , 4 );
}
    
        出于一个奇怪的原因,我并没有真正理解我已经通过将autoAdjustsScrollViewInsets设置为NO(或Interface Builder中的等效项)来解决此问题。这是iOS 8.1。     
        我遇到了一个自定义字体的问题,并通过在键盘事件触发时将标签移到另一个方向来解决此问题。我通过覆盖
drawRect:
方法移动了标签中心
- (void)drawRect:(CGRect)rect
{
    self.titleLabel.center = CGPointMake(self.titleLabel.center.x, self.titleLabel.center.y+3);
}
    
        这是标准UITextField中的预期行为。但是,您可以通过子类化UITextField并通过调整文本本身的边界来解决此问题。 迅捷3
override func textRect(forBounds bounds: CGRect) -> CGRect {
    return(bounds.insetBy(dx: 0, dy: 0))
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
    return(bounds.insetBy(dx: 0, dy: -0.5))
}

override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
    return(bounds.insetBy(dx: 0, dy: 0))
}
这应该可以解决问题!     
        在IB中将文本字段的边框样式设置为\“ none \”以外的任何值,然后在ViewController \的viewDidLoad中设置:
yourTextField.borderStyle = .none
(基于Box Jeon的回答)     
        迅捷3 不要忘记UITextField的“ 19”。如果需要有效的实现,则需要考虑* rect(forBounds:...)函数的20%。并且还要确保仅将越野车
iOS 10
的整流器移位,而不是将9或8移位。以下代码可以解决问题:
public class CustomTextField: UITextField {
    public override func textRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.textRect(forBounds: bounds)

        if #available(iOS 10, *) {
            return superValue.insetBy(dx: 0, dy: 0)
        }
        return superValue
    }

    public override func editingRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.editingRect(forBounds: bounds)

        if #available(iOS 10, *) {
            return superValue.insetBy(dx: 0, dy: -0.5)
        }
        return superValue
    }

    public override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.placeholderRect(forBounds: bounds)

        if #available(iOS 10, *) {
            if isEditing {
                return superValue.insetBy(dx: 0, dy: 0.5)
            }
            return superValue.insetBy(dx: 0, dy: 0.0)
        }
        return superValue
    }
}
编辑 我从上面到下面对代码进行了少量编辑,对我来说效果更好。我在iPhone 6、6s,7、7s以及带有iOS 9.3和10.3的'plus'设备上进行测试。
public class CustomTextField: UITextField {

    public override func textRect(forBounds bounds: CGRect) -> CGRect {
        let superValue = super.textRect(forBounds: bounds)

        if #available(iOS 10, *) {
            return superValue.insetBy(dx: 0, dy: -0.3)
        }
        return superValue.insetBy(dx: 0, dy: -0.2)
    }

    public override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return self.textRect(forBounds: bounds)
    }
}
我认为这也取决于您使用的字体。我用
UIFont.systemFont(ofSize: 17.0, weight: UIFontWeightLight)
    
        您应将Font属性设置为早于Text属性。     

要回复问题请先登录注册