我正在开发一个阿拉伯语的应用程序,我有UITextField和textAlignment权限。现在我想在左侧显示textField的清除按钮。是否可以在不添加自定义按钮的情况下执行此操作?
当前位置
期望的位置
答案 0 :(得分:8)
使用以下类别并确保您的文字对齐方式正确:)
@interface UICrossButtonTextField:UITextField
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
@end
@implementation UICrossButtonTextField
- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
CGRect originalRect = [super clearButtonRectForBounds:bounds];
return CGRectOffset(originalRect, -originalRect.origin.x+5, 0); }
- (CGRect)editingRectForBounds:(CGRect)bounds {
CGRect originalRect = [super clearButtonRectForBounds:bounds];
bounds = CGRectMake(originalRect.size.width, bounds.origin.y, bounds.size.width-originalRect.size.width, bounds.size.height);
return CGRectInset(bounds, 13, 3);
}
@end
答案 1 :(得分:2)
虽然我建议您检查this answer处理从左到右的应用语言,但作为一种解决方法,您可以关注userar's answer,以下代码段是 Swift 3 他的回答版本:
创建自定义UITextField类,如下所示:
class CustomTextField: UITextField {
private var originalRect = CGRect.zero
override func awakeFromNib() {
super.awakeFromNib()
originalRect = super.clearButtonRect(forBounds: bounds)
clearButtonMode = .whileEditing
textAlignment = .right
}
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
return originalRect.offsetBy(dx: -originalRect.origin.x + 5, dy: 0)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
let bounds = CGRect(x: originalRect.size.width, y: bounds.origin.y, width: bounds.size.width-originalRect.size.width, height: bounds.size.height)
return bounds.insetBy(dx: 13, dy: 3)
}
}
输出结果为:
答案 2 :(得分:0)
SWIFT 3语法:
class TextFields: UITextField {
// You will need this
private var firstPlace = CGRect.zero
override func awakeFromNib() {
super.awakeFromNib()
firstPlace = super.clearButtonRect(forBounds: bounds) // to access clear button properties
/* uncomment these following lines if you want but you can change them in main.storyboard too
clearButtonMode = .whileEditing // to show the clear button only when typing starts
textAlignment = .right // to put the text to right side
*/
}
// Function to change the clear button
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
return firstPlace.offsetBy(dx: -firstPlace.origin.x + 5, dy: 0)
}
}
希望它有效