我想在UITextField的开头留出一点空间,就像下面这样: Add lefthand margin to UITextField

但我不知道如何使用Swift .

推荐答案

这就是我现在使用的:

Swift 4.2

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
}

Swift 4

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }
}

Swift 3:

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }
}

我从来没有设置过其他的填充物,但是你可以调整.该类不处理文本字段上的right View和leftView.如果希望正确处理,可以使用类似于(objc中的示例,我只需要rightView:

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets);

    if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) {
        return [self adjustRectWithWidthRightView:paddedRect];
    }
    return paddedRect;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets);

    if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) {
        return [self adjustRectWithWidthRightView:paddedRect];
    }
    return paddedRect;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets);

    if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeWhileEditing) {
        return [self adjustRectWithWidthRightView:paddedRect];
    }
    return paddedRect;
}

- (CGRect)adjustRectWithWidthRightView:(CGRect)bounds {
    CGRect paddedRect = bounds;
    paddedRect.size.width -= CGRectGetWidth(self.rightView.frame);

    return paddedRect;
}

Ios相关问答推荐

崩溃到初始化react -带网页的原生iOS应用程序

在SwiftData中从@ Query创建可排序、有序和分组的数据

如何使视图完全适合屏幕,而不会在旋转时溢出手机屏幕的边界?

在某些情况下,UIHostingController在自动调整时不能调整大小

如何在SwiftUI中拥有基于操作系统版本的条件修饰符?

withTaskGroup不适用于CLGeocoder

如何在后台显示 Swift UI 中的通讯通知?

当 .searchable 修饰符处于活动状态时,如何将变量设置为 false?

如何通过点击按钮滚动到 ScrollView 中的特定视图?

Swift Combine和iOS版本限制?

在 SwiftUI 中放置全局 NavigationPath 的位置

如何从 Locale Swift 获取货币符号

包装在 AnyView 中时 UIViewControllerRepresentable 无法正常工作

如何在 iPhone 中为透明的 PNG 图像着色?

iOS - 确保在主线程上执行

如何用 Swift 圆化 UILabel 的边缘

无论我将构建版本或应用程序版本更改为什么,都会出现 ITEMS-4238冗余二进制上传错误

是否可以在 iOS 9 上让您的 iPad 应用退出多任务处理?

Swift 中的日期到毫秒和回溯到日期

/usr/bin/codedesign 失败,退出代码为 1