ios – UITextField文本在左侧不滚动

前端之家收集整理的这篇文章主要介绍了ios – UITextField文本在左侧不滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UITextField子类,我已经覆盖了一些方法(参见下面的代码).问题是,当我输入它并且文本达到严格边距时,它将不再显示我正在键入的内容.在视图调试模式中,我看到UIFieldEditor比文本提交的要宽得多.

这是UITextFiled子类代码

  1. - (instancetype)init
  2. {
  3. self = [super init];
  4. if (self) {
  5. self.edgeInsets = UIEdgeInsetsMake(7,10,15);
  6. }
  7. return self;
  8. }
  9.  
  10. - (id)initWithFrame:(CGRect)frame{
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. self.edgeInsets = UIEdgeInsetsMake(7,15);
  14. }
  15. return self;
  16. }
  17.  
  18. -(id)initWithCoder:(NSCoder *)aDecoder{
  19. self = [super initWithCoder:aDecoder];
  20. if(self){
  21. self.edgeInsets = UIEdgeInsetsMake(7,15);
  22. }
  23. return self;
  24. }
  25.  
  26. - (CGRect)textRectForBounds:(CGRect)bounds {
  27. return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,self.edgeInsets)];
  28. }
  29.  
  30. - (CGRect)editingRectForBounds:(CGRect)bounds {
  31. return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds,self.edgeInsets)];
  32. }

这是我使用这个类的代码

  1. // alloc / init
  2. _myTextField.delegate = self;
  3. _myTextField.backgroundColor = [UIColor whiteColor];
  4. _myTextField.layer.masksToBounds = YES;
  5. _myTextField.layer.cornerRadius = 3.0;
  6. _myTextField.returnKeyType = UIReturnKeyDone;
  7. _myTextField.clearButtonMode = UITextFieldviewmodeWhileEditing;
  8. _myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  9. _myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
  10. _myTextField.hidden = YES;
  11.  
  12. _tfButton = [UIButton buttonWithType:UIButtonTypeSystem];
  13. [_tfButton setImage:[UIImage imageNamed:@"ic_edit"] forState:UIControlStateNormal];
  14. [self fs_addSubview:_tfButton];
  15. [_tfButton constrainWidth:22.];
  16. [_tfButton setTintColor:[UIColor greenColor]];
  17. [_tfButton constrainHeight:22.];
  18. [_tfButton constrainToRightOfView:_myTextField margin:-25.0];
  19. [_tfButton constrainEqualCenterYWithView:_myTextField offset:0.0];
  20. [_tfButton setUserInteractionEnabled:NO];
  21.  
  22.  
  23. UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0,5,5)];
  24.  
  25. _myTextField.leftView = leftView;
  26. _myTextField.leftviewmode = UITextFieldviewmodeAlways;

用户在此文本字段中键入大字符串时,如何让文本在左侧滚动?

解决方法

你的代码是完全正确的.问题来自UITextField类.
当您在故事板中放入UITextField时,将其fontSize和minFontSize设置为50.此系统textField将与您的自定义textField具有相同的问题.

这只是因为UITextField不知道如何在狭窄的空间中绘制大字体.

我使用了textField的子类(默认高度为30),在将字体大小更改为7之后,一切正常.

猜你在找的iOS相关文章