我有一个UITextField子类,我已经覆盖了一些方法(见下面的代码).问题是,当我输入它并且文本达到严格边距时,它将不再显示我正在键入的内容.在视图调试模式中,我看到UIFieldEditor文本提交的范围要宽得多.
这是UITextFiled子类代码:
- (instancetype)init
{
self = [super init];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15);
}
return self;
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15);
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
self.edgeInsets = UIEdgeInsetsMake(7, 0, 10, 15);
}
return self;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}
Run Code Online (Sandbox Code Playgroud)
这是我使用这个类的代码:
// alloc / init
_myTextField.delegate = self;
_myTextField.backgroundColor = [UIColor whiteColor];
_myTextField.layer.masksToBounds = YES;
_myTextField.layer.cornerRadius = 3.0;
_myTextField.returnKeyType = UIReturnKeyDone;
_myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
_myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
_myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
_myTextField.hidden = YES;
_tfButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_tfButton setImage:[UIImage imageNamed:@"ic_edit"] forState:UIControlStateNormal];
[self fs_addSubview:_tfButton];
[_tfButton constrainWidth:22.];
[_tfButton setTintColor:[UIColor greenColor]];
[_tfButton constrainHeight:22.];
[_tfButton constrainToRightOfView:_myTextField margin:-25.0];
[_tfButton constrainEqualCenterYWithView:_myTextField offset:0.0];
[_tfButton setUserInteractionEnabled:NO];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
_myTextField.leftView = leftView;
_myTextField.leftViewMode = UITextFieldViewModeAlways;
Run Code Online (Sandbox Code Playgroud)
当用户在此文本字段中键入大字符串时,如何让文本在左侧滚动?
wj2*_*061 10
你的代码是完全正确的.问题来自UITextField班级.当您放入UITextField故事板中的a ,并将其fontSize和minFontSize设置为50.此系统textField将与您的自定义textField具有相同的问题.
这只是因为UITextField不知道如何在狭窄的空间中绘制大字体.
我使用了textField的子类(默认高度为30),在将字体大小更改为7之后,一切正常.
我遇到了同样的问题,但我没有任何 UITextField 子类。问题是文本字段在文本字段高度和字体大小之间有一定的比例。将字体大小增加到高于特定值 wrt 文本字段高度会导致此问题。我所做的是稍微减小字体大小,然后文本字段再次开始工作。
| 归档时间: |
|
| 查看次数: |
1671 次 |
| 最近记录: |