Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITextField不';在子类中实现textRectForBounds和editingRectForBounds后,是否滚动到末尾?_Ios_Objective C_Uitextfield - Fatal编程技术网

Ios UITextField不';在子类中实现textRectForBounds和editingRectForBounds后,是否滚动到末尾?

Ios UITextField不';在子类中实现textRectForBounds和editingRectForBounds后,是否滚动到末尾?,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我有一个UITextField子类,我在其中实现以下方法: - (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds , 30, 7); } -(CGRect) editingRectForBounds:(CGRect)bounds { return CGRectInset(bounds , 30, 7); } 由于这两种方法,当输入的文本大于UITextField的宽度时,UITextField不

我有一个UITextField子类,我在其中实现以下方法:

- (CGRect)textRectForBounds:(CGRect)bounds {

  return CGRectInset(bounds , 30, 7);
}

-(CGRect) editingRectForBounds:(CGRect)bounds {
  return CGRectInset(bounds , 30, 7);
}

由于这两种方法,当输入的文本大于
UITextField
的宽度时,
UITextField
不会滚动到末尾。有什么解决方法吗?

这可能是您的自定义
UITextField
CALayer
插入高度和字体大小之间的冲突

您可以尝试以下调整之一:

  • 减小字体大小
  • 增加
    情节提要上的
    UITextField
    高度,或以编程方式在子类中增加,如:

    - (void)drawRect:(CGRect)rect {
        CGRect frameRect = self.frame;
        frameRect.size.height = 35 //specify the height you want;
        self.frame = frameRect;
     }
    
  • 减小
    CGRectInset
    上的y坐标值

  • 或者使用
    UIEdgeInsets
    对设置值进行更多控制​​每个职位的职位。比如:

    - (CGRect)textRectForBounds:(CGRect)bounds{
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 30, 5, 30);
        return UIEdgeInsetsInsetRect(bounds, contentInsets);
    }
    
    - (CGRect)editingRectForBounds:(CGRect)bounds{
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(5, 30, 5, 30);
        return UIEdgeInsetsInsetRect(bounds, contentInsets);
    }
    

原因-您对文本字段文本使用固定字体大小。 用这个

 textFieldObject.adjustsFontSizeToFitWidth = YES;  
简明扼要。如果要设置最小字体大小,可以使用此选项

 textFieldObject.minimumFontSize

享受

我不确定这是你想要的,但是用下面的方法修改
编辑rectforbounds
会使插入变得动态,所以一旦文本框变“满”,就会减少插入,这样文本就会填满整个文本框

假设
textInset\u
是所需的插图:

- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGFloat compensate = .0f;
    CGSize size = [self.text sizeWithFont:self.font];
    CGFloat textboxWidth = CGRectGetWidth(self.width);

    // If size of text plus inset (right side) is bigger
    // than textbox's width, don't set an inset
    if (size.width+textInset_ >= textboxWidth) {
        compensate = 0;
    }

    // If text area (textbox width minus two insets) is less than
    // text size, lessen the inset so the text fits in 
    else if (textboxWidth - textInset_*2 < size.width) {
        compensate = fabs(size.width - (textboxWidth - textInset_));
    }

    // Text fits in textbox WITH insets
    else {
        compensate = textInset_;
    }
    return CGRectInset(bounds, compensate, 10);
}
-(CGRect)编辑rectforbounds:(CGRect)bounds{
CGFloat补偿=.0f;
CGSize size=[self.text sizeWithFont:self.font];
CGFloat textboxWidth=CGRectGetWidth(self.width);
//如果文本加插图(右侧)的大小更大
//超过文本框的宽度,不要设置插入
如果(size.width+textInset_>=textboxWidth){
补偿=0;
}
//如果文本区域(文本框宽度减去两个插图)小于
//文本大小,减少插入,使文本适合
else if(textboxWidth-textInset_*2
它不起作用仅仅是因为您在
UITextField
中声明的文本的字体大小以及您正在应用的
CGRectInset
。实际上,它应该是根据您正在设置的字体大小。例如,对于字体大小14.0,只需按以下方式更改方法:

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds , 30, 6);
}
-(CGRect) editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds , 30, 6);
}
原因是绘制文本的矩形。例如,当我们将
UITextField
的字体大小设置为14.0时,至少需要18.0的高度来调整框架中的文本。所以它需要在文本周围额外增加4个像素,你可以说它覆盖了框架。因此,在您的情况下,此条件并不令人满意,因为据我猜测,您已将字体大小调整为14.0,并且默认情况下,
UITextField
的帧高度为30.0,并且您的边缘遮罩每边为7.0,这意味着总共为14.0。因此,文本的rect返回16.0,这反过来会阻止操作系统更新文本框,因为它不在图形上下文中。我希望你是清楚的。如果它解决了你的问题,那么请接受我的回答。更多的好答案如下:

- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds , 30, (30 - self.font.pointSize - 4)/2);
}

-(CGRect) editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds , 30, (30 - self.font.pointSize - 4)/2);
}
享受吧

关于此链接:


您是否在实现这些功能以在文本周围创建插图?还有其他方法可以实现这一点,也许不那么麻烦。你确定你没有凌驾于其他东西之上吗?因为我用一个自定义的文本域做了一个简单的测试项目,只是用上面的两个方法覆盖,文本水平滚动到最后。如果你把文本域直接放在xib中,它就正常工作了,因为adjustsFontSizeToFitWidth选项已经设置为“是”。但是,如果要动态分配textfield类,则必须设置textFieldObject.adjustsFontSizeToFitWidth=YES;你试过我的答案吗?我在这里看到的一个问题是,这个答案认为
-[UIFont pointSize]
可以与像素距离互换。这不是你投反对票的原因。检查答案,然后进行向下投票。字体大小用于计算特定字体大小的UITextField的最大边缘掩码。据我所知,字体大小到像素的转换完全取决于iOS是否为视网膜/普通。请不要编辑我的答案,使其毫无意义。。这些数字和我写的一模一样。
step 1 - make a custom text field MyTextField first

@interface MyTextField : UITextField
step 2 - override MyTextField methods as per your requirement

///place holder position

- (CGRect)placeholderRectForBounds:(CGRect)bounds
 {
   return CGRectInset(bounds, 8, 8);
 }
 // text position
 - (CGRect)textRectForBounds:(CGRect)bounds {

  return CGRectInset(bounds, 8,4);
 }

 // text position while editing
 - (CGRect)editingRectForBounds:(CGRect)bounds {

     return CGRectInset(bounds, 8, 4);
 }
step 3- import MyTextField in your controller class & make object of MyTextField

#import "MyTextField.h"

-(void) viewWillAppear:(BOOL)animated {

    MyTextField* textfield1 = [[MyTextField alloc] init];
    // and so on

}