Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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
Objective c textRectForBounds:在从Nib加载的自定义UITextFields上未调用_Objective C_Ios_Uitextfield - Fatal编程技术网

Objective c textRectForBounds:在从Nib加载的自定义UITextFields上未调用

Objective c textRectForBounds:在从Nib加载的自定义UITextFields上未调用,objective-c,ios,uitextfield,Objective C,Ios,Uitextfield,我创建了UITextField的一个子类,它覆盖[text | editing | placeholder]RectForBounds:(都使用完全相同的实现)。当从代码实例化时,一切都可以正常工作,但当从nib加载时就不行了。在从nib加载的文本字段上,既不绘制文本也不绘制占位符 我注意到,在用户点击文本字段之前,不会调用任何RectForBounds方法;点击时,使用正确的边界大小({0,0},{300,39}})调用编辑和占位符rectforbounds,而使用({0,0},{100,10

我创建了UITextField的一个子类,它覆盖[text | editing | placeholder]RectForBounds:(都使用完全相同的实现)。当从代码实例化时,一切都可以正常工作,但当从nib加载时就不行了。在从nib加载的文本字段上,既不绘制文本也不绘制占位符

我注意到,在用户点击文本字段之前,不会调用任何RectForBounds方法;点击时,使用正确的边界大小({0,0},{300,39}})调用编辑和占位符rectforbounds,而使用({0,0},{100,100})调用textRectForBounds

那么,为什么从不绘制文本(并且只在从nib加载的字段上绘制)?我是做错了什么,还是这是UITextField的错误

编辑:

代码如下:

#import "SearchTextField.h"
#import <QuartzCore/QuartzCore.h>

#define BUTTON_WIDTH 50
#define MAX_LABEL_WIDTH 200

@interface SearchTextField ()

@property(retain, nonatomic) UILabel *label;
@property(retain, nonatomic) UIButton *button;

- (void)i_baseInit;

@end

@implementation SearchTextField

@synthesize title = _title;
@synthesize label = label_;
@synthesize button = button_;
@synthesize showButton = _showButton;
@synthesize backgroundView = _my_backgroundView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _showButton = YES;
        [self i_baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self i_baseInit];
    }
    return self;
}

- (void)i_baseInit
{
    self.opaque = NO;
    self.textAlignment = UITextAlignmentRight;
    self.leftViewMode = UITextFieldViewModeAlways;
    self.rightViewMode = UITextFieldViewModeAlways;
    self.backgroundColor = [UIColor clearColor];

    _my_backgroundView = [[MyView alloc] init];
    self.backgroundView.backgroundColor = [UIColor whiteColor];

    self.label = [[[UILabel alloc] init] autorelease];
    self.label.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:15];
    self.label.textColor = [UIColor darkGrayColor];
    self.label.textAlignment = UITextAlignmentRight;
    self.label.backgroundColor = [UIColor clearColor];
    self.label.text = _title;
    self.leftView = self.label;

    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setBackgroundImage:[UIImage imageNamed:@"button_sm"]
                           forState:UIControlStateNormal];
    [self.button setBackgroundImage:[UIImage imageNamed:@"button_sm_pressed"]
                           forState:UIControlStateHighlighted];
    self.button.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
    self.button.hidden = !_showButton;
    self.rightView = self.button;
}

- (void)drawRect:(CGRect)rect
{
    self.backgroundView.frame = CGRectMake(self.bounds.origin.x,
                                           self.bounds.origin.y,
                                           self.bounds.size.width-1, // Without this, the background sticks out 1 pt from the button
                                           self.bounds.size.height);
    [self.backgroundView drawRect:rect];
}

- (id)copy
{
    SearchTextField *copy = [[self.class alloc] initWithFrame:self.frame];
    copy.text = self.text;
    copy.textAlignment = self.textAlignment;
    copy.textColor = self.textColor;

    copy.placeholder = self.placeholder;

    copy.background = self.background;
    copy.backgroundColor = self.backgroundColor;

    copy.borderStyle = self.borderStyle;
    copy.font = self.font;
    copy.showButton = self.showButton;
    copy.title = self.title;

    copy.delegate = self.delegate;

    copy.secureTextEntry = self.secureTextEntry;

    copy.returnKeyType = self.returnKeyType;
    copy.keyboardType = self.keyboardType;

    return copy;
}

#pragma mark - UITextField Positioning -

- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGSize labelSize = [self.label.text sizeWithFont:self.label.font constrainedToSize:CGSizeMake(MAX_LABEL_WIDTH, bounds.size.height)];
    CGSize textSize = [@"Test" sizeWithFont:self.font];
    CGRect rect = CGRectMake(10 + labelSize.width,
                      (bounds.size.height-textSize.height)/2,
                      bounds.size.width - (20 + labelSize.width + ((self.showButton) ? BUTTON_WIDTH : 0)),
                      textSize.height);
    return rect;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    return [self textRectForBounds:bounds];
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    return [self textRectForBounds:bounds];
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
    return CGRectMake(10,
                      0,
                      [self.label.text sizeWithFont:self.label.font constrainedToSize:CGSizeMake(MAX_LABEL_WIDTH, bounds.size.height)].width,
                      bounds.size.height);
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds
{
    if (!self.showButton) { return CGRectZero; }

    return CGRectMake(bounds.size.width-BUTTON_WIDTH,
                      0,
                      BUTTON_WIDTH,
                      bounds.size.height);
}

#pragma mark -

- (void)setTitle:(NSString *)title
{
    [_title release];
    _title = [title retain];
    self.label.text = title;
    [self setNeedsLayout];
}

- (UIImage *)imageForState:(UIControlState)state
{
    return [self.button imageForState:state];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [self.button setImage:image forState:state];
}

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    [self.button addTarget:target action:action forControlEvents:controlEvents];
}

- (void)setShowButton:(BOOL)showButton
{
    self.button.hidden = !showButton;
    _showButton = showButton;
    [self setNeedsLayout];
}

- (MyView *)backgroundView
{
    [self setNeedsDisplay];
    return _my_backgroundView;
}
- (void)dealloc
{
    [_my_backgroundView release];
    [label_ release];
    [button_ release];

    [super dealloc];
}

@end
#导入“SearchTextField.h”
#进口
#定义按钮宽度50
#定义最大标签宽度200
@接口SearchTextField()
@属性(保留,非原子)UILabel*标签;
@属性(保留,非原子)UIButton*按钮;
-(无效)i_baseInit;
@结束
@实现SearchTextField
@综合标题=_标题;
@综合标签=标签;
@合成按钮=按钮;
@综合showButton=\u showButton;
@综合背景视图=\u我的\u背景视图;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我)
{
_showButton=是;
[self i_baseInit];
}
回归自我;
}
-(id)initWithCoder:(NSCoder*)aDecoder
{
if(self=[super initWithCoder:aDecoder])
{
[self i_baseInit];
}
回归自我;
}
-(无效)i_baseInit
{
self.不透明=否;
self.textAlignment=UITextAlignmentRight;
self.leftViewMode=UITextFieldViewModeAlways;
self.rightViewMode=UITextFieldViewModeAlways;
self.backgroundColor=[UIColor clearColor];
_my_backgroundView=[[MyView alloc]init];
self.backgroundView.backgroundColor=[UIColor whiteColor];
self.label=[[UILabel alloc]init]autorelease];
self.label.font=[UIFont fontWithName:@“HelveticaNeue CondensedBold”大小:15];
self.label.textColor=[UIColor darkGrayColor];
self.label.textAlignment=UITextAlignmentRight;
self.label.backgroundColor=[UIColor clearColor];
self.label.text=\u title;
self.leftView=self.label;
self.button=[UIButton buttonWithType:UIButtonTypeCustom];
[self.button setBackgroundImage:[UIImage ImageName:@“button_sm”]
forState:uicontrol状态正常];
[self.button setBackgroundImage:[UIImage ImageName:@“按钮按下”]
forState:uicontrol状态突出显示];
self.button.autoresizingMask=UIViewAutoresizingFlexibleHeight | uiviewautoresizingflexibleleleftmargin;
self.button.hidden=!\u showButton;
self.rightView=self.button;
}
-(void)drawRect:(CGRect)rect
{
self.backgroundView.frame=CGRectMake(self.bounds.origin.x,
自界,原点,y,
self.bounds.size.width-1,//如果没有此选项,背景从按钮伸出1磅
自限、大小、高度);
[self.backgroundView drawRect:rect];
}
-(id)副本
{
SearchTextField*copy=[[self.class alloc]initWithFrame:self.frame];
copy.text=self.text;
copy.textAlignment=self.textAlignment;
copy.textColor=self.textColor;
copy.placeholder=self.placeholder;
copy.background=self.background;
copy.backgroundColor=self.backgroundColor;
copy.borderStyle=self.borderStyle;
copy.font=self.font;
copy.showButton=self.showButton;
copy.title=self.title;
copy.delegate=self.delegate;
copy.secureTextEntry=self.secureTextEntry;
copy.returnKeyType=self.returnKeyType;
copy.keyboardType=self.keyboardType;
返回副本;
}
#pragma标记-UITextField定位-
-(CGRect)textRectForBounds:(CGRect)bounds
{
CGSize labelSize=[self.label.text sizeWithFont:self.label.font constrainedToSize:CGSizeMake(最大标签宽度,边界.大小.高度)];
cgsizetextsize=[@“测试”大小:self.font];
CGRect rect=CGRectMake(10+labelSize.width,
(bounds.size.height text size.height)/2,
bounds.size.width-(20+labelSize.width+((self.showButton)?按钮宽度:0)),
文本大小、高度);
返回矩形;
}
-(CGRect)占位符rectforbounds:(CGRect)bounds
{
返回[self-textRectForBounds:bounds];
}
-(CGRect)编辑rectforbounds:(CGRect)bounds
{
返回[self-textRectForBounds:bounds];
}
-(CGRect)leftViewRectForBounds:(CGRect)bounds
{
返回CGRectMake(10,
0,
[self.label.text sizeWithFont:self.label.font constrainedToSize:CGSizeMake(最大标签宽度,边界.大小.高度)].WIDTH,
边界、大小、高度);
}
-(CGRect)rightViewRectForBounds:(CGRect)bounds
{
如果(!self.showButton){return CGRectZero;}
返回CGRectMake(bounds.size.width-BUTTON_width,
0,
按钮宽度,
边界、大小、高度);
}
#布拉格标记-
-(void)setTitle:(NSString*)标题
{
[_标题发布];
_所有权=[所有权保留];
self.label.text=标题;
[self-setNeedsLayout];
}
-(UIImage*)imageForState:(UIControlState)状态
{
返回[self.button imageForState:state];
}
-(void)设置图像:(UIImage*)图像状态:(UIControlState)状态
{
[self.button setImage:image for state:state];
}
-(无效)添加目标:(id)目标操作:(SEL)controlEvents的操作:(uicontrol事件)controlEvents
{
[self.button addTarget:target action:action for controlEvents:controlEvents];
}
-(无效)设置显示按钮:(BOOL)显示按钮
{
self.button.hidden=!showButton;
_showButton=showButton;
[self-setNeedsLayout];
}
-(MyView*)背景视图
{
[自我设置需要显示];
返回我的背景视图;
}
-(无效)交易
- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect rect ;
    rect = [super textRectForBounds:bounds];
    NSLog(@"REAL textRectForBounds %@", NSStringFromCGRect(rect) );

    CGSize labelSize = [self.label.text sizeWithFont:self.label.font constrainedToSize:CGSizeMake(MAX_LABEL_WIDTH, bounds.size.height)];
    CGSize textSize = [@"Test" sizeWithFont:self.font]; 

    NSLog(@"labelSize=%@ textSize=%@ bounds=%@", NSStringFromCGSize(labelSize), NSStringFromCGSize(textSize), NSStringFromCGRect(bounds));

    rect = CGRectMake(10 + labelSize.width,
                      (bounds.size.height-textSize.height)/2,
                      bounds.size.width - (20 + labelSize.width + ((self.showButton) ? BUTTON_WIDTH : 0)),
                      textSize.height);

NSLog(@"%@: textRectForBounds %@", type, NSStringFromCGRect(rect) );
    return rect;
}
labelSize={0, 100} textSize={28, 18} bounds={{0, 0}, {100, 100}}
textRectForBounds {{10, 41}, {80, 18}}