0

[text | editing | placeholder] RectForBounds :(すべてまったく同じ実装)をオーバーライドするUITextFieldのサブクラスを作成しました。コードからインスタンス化した場合はすべて正常に機能しますが、ペン先からロードした場合は機能しません。ペン先から読み込まれたテキストフィールドには、テキストもプレースホルダーも描画されません。

ユーザーがテキストフィールドをタップするまで、RectForBoundsメソッドは呼び出されないことに気づきました。タップすると、editing&placeholderRectForBoundsは正しい境界サイズ({{0、0}、{300、39}})で呼び出され、textRectForBoundsは({{0、0}、{100、100}})で呼び出されます。

では、なぜテキストが描画されないのですか(そしてペン先からロードされたフィールドにのみ)?私は何か間違ったことをしていますか、それともこれは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
4

1 に答える 1

0

問題は、Appleのコードでは、initWithCoderとinitWithFrameでわずかに異なるパスを使用するため、プレースホルダー文字列とは関係なく、このメソッドを呼び出すことです。

したがって、私のテストでは、2つのラベルがありました。1つはコードで作成され、もう1つはペン先で作成され、どちらも適度に長いプレースホルダー文字列を持っています。

表示されるプレースホルダーに関連しない追加のメッセージがあるため、主要な問題は「textRectForBounds:」に関連しています。それだけでなく、境界は巨大であり、それはあなたがコントロールの境界の外にあるrectを返す原因になっています。私はあなたのコードを十分に理解して解決策を提案することはできませんが、少なくともあなたは今何を修正すべきかを知っています。

この追加のロギングを使用すると:

- (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}}

したがって、システムは何らかの理由でその100x100の境界を渡し、コードはtextFieldの10ポイント下にyオフセットを持つrectを作成します。これを説明する必要があります。

于 2012-08-14T21:40:29.500 に答える