ios - 更改 UITextField 中的文本布局

标签 ios swift xcode text uitextfield

下面是一些 UITextFields 的图像。关于底部的大文本,我如何让它从左上角(而不是中间)开始文本,以及如何让它们全部从右边开始。如您所见,它们笨拙地靠近左边缘。


enter image description here

最佳答案

对于 textField,覆盖以下方法:

class InsetTextField: UITextField {

    var inset: CGFloat = 10

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

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

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

}

对于 TextView :

textView.contentInset = UIEdgeInsetsMake(inset, inset, inset, inset);

当 textView 完成编辑时,您可以参与,查看 this question .

关于ios - 更改 UITextField 中的文本布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42323588/

相关文章:

ios - reloadData 后未调用 cellForRowAtIndexPath

ios - Chartboost 插页式 : "not in the window hierarchy!" warning

iphone - 如何根据需要格式化字符串

ios - 在 Swift 中将叠加效果从 psd 转换为 UIImage

IOS10 将所有 xcode 控制台日志打印到 iPhone 应用程序页面内的 UITextview

ios - 如何计算设备移动的最后两点的加速度计

ios - 删除特定数组元素,等于字符串 - Swift

c++ - 代码块的命令行工具

iPhone:将 FPPopover 类与 UIBarButtonItem 一起使用时出错

iphone - 复制dylib