-(CGRect)textRectForBounds:(CGRect)bounds没有被调用 [英] - (CGRect) textRectForBounds:(CGRect)bounds not being called

查看:350
本文介绍了-(CGRect)textRectForBounds:(CGRect)bounds没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UITextField内移动文本,以使其正确垂直对齐.我已经在网上搜索过,所有内容都指向编辑这些方法:

I am trying to move text inside a UITextField so that it is vertically aligned correctly. I have searched online and everything points to editing these methods:

- (CGRect) textRectForBounds:(CGRect)bounds

- (CGRect) editingRectForBounds:(CGRect)bounds

尽管我已经像这样编辑它们:

Though I have edited them like this:

- (CGRect) textRectForBounds:(CGRect)bounds {
    NSLog(@"hi");
    return CGRectInset(bounds, 10, 10);
}

尽管它没有进入nslog.

Though it doesn't get to the nslog.

我也尝试在文本字段上调用该方法,但没有成功

I have also tried calling the method on the textfield but to no success

这是初始化文本视图并进行设置的方式:

This is how I am initialising the text views and setting them up:

-(void) viewWillAppear:(BOOL)animated {
    textfield1 = [[UITextField alloc] init];
    textfield2 = [[UITextField alloc] init];
    textfield3 = [[UITextField alloc] init];
    textfield4 = [[UITextField alloc] init];
    for (UITextField *text in [NSArray arrayWithObjects:textfield1, textfield2, textfield3, textfield4, nil]) {
        text.delegate = self;
        text.placeholder = [placeholders objectAtIndex:i];
        text.frame = CGRectMake(10, offsetTop, container.frame.size.width - 20, 40);
        [text textRectForBounds:text.bounds];
        [text editingRectForBounds:text.bounds];
        text.borderStyle = UITextBorderStyleRoundedRect;
        offsetTop += 50;
        i++;
        [container addSubview:text];
    }
}

推荐答案

步骤1-首先创建自定义文本字段MyTextField

step 1 - make a custom text field MyTextField first

@interface MyTextField : UITextField

第2步-根据您的要求覆盖MyTextField方法

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);
 }

第3步-在控制器类&中导入MyTextField.制作MyTextField的对象

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

}

现在每个文本字段都将按预期工作.

now every text field will work as expected.

这篇关于-(CGRect)textRectForBounds:(CGRect)bounds没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆