【发布时间】:2017-02-19 04:19:28
【问题描述】:
【问题讨论】:
-
做左填充。对于文本视图,您必须使用左插图。
标签: ios swift xcode text uitextfield
【问题讨论】:
标签: ios swift xcode text uitextfield
对于 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.contentInset = UIEdgeInsetsMake(inset, inset, inset, inset);
您可以在 textView 完成编辑后参与其中,请查看this question。
【讨论】:
您可以转接至自定义 TextField:
import UIKit
class CustomTextField: UITextField {
var inset:CGFloat = 12 // You can set the inset you want
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)
}
}
结果,你可以看到CustomTextField的插图:
编辑
【讨论】:
默认情况下,UITextFields 只有一行。根据您的图片,我假设您希望用户有空间输入段落。对于段落,最好使用 UITextViews。对于您的问题,这将是一个更简单的解决方案。
我总是将 TextViews 用于传记,因为它让我的生活更轻松。
【讨论】:
添加文本字段的左填充
let paddingVie = UIView(frame: CGRect(x: 0, y:0, width: 10, height: 10))
yourtextField.leftView = paddingVie
yourtextField.leftViewMode = .always
为文本视图添加
yourTextVieName.textContainerInset =
UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right
【讨论】: