首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >- (CGRect) textRectForBounds:(CGRect)边界未被调用

- (CGRect) textRectForBounds:(CGRect)边界未被调用
EN

Stack Overflow用户
提问于 2014-03-08 17:45:04
回答 1查看 4.6K关注 0票数 1

我试图在UITextField中移动文本,使其垂直对齐。我在网上搜索过,所有内容都指向编辑这些方法:

代码语言:javascript
复制
- (CGRect) textRectForBounds:(CGRect)bounds

代码语言:javascript
复制
- (CGRect) editingRectForBounds:(CGRect)bounds

虽然我这样编辑过它们:

代码语言:javascript
复制
- (CGRect) textRectForBounds:(CGRect)bounds {
    NSLog(@"hi");
    return CGRectInset(bounds, 10, 10);
}

虽然它没有到达nslog。

我也尝试过在textfield上调用该方法,但没有成功。

我就是这样初始化文本视图并设置它们的:

代码语言:javascript
复制
-(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];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-08 18:48:13

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

代码语言:javascript
复制
@interface MyTextField : UITextField

步骤2-根据需要重写MyTextField方法

代码语言:javascript
复制
///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的对象

代码语言:javascript
复制
#import "MyTextField.h"

-(void) viewWillAppear:(BOOL)animated {

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

}

现在,每个文本字段都将像预期的那样工作。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22272800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档