Отобразить индикатор выполнения внутри объекта UITextField - PullRequest
1 голос
/ 11 июня 2011

Как можно нарисовать индикатор прогресса внутри UITextField? До сих пор я тестировал два способа.

1. Добавьте объект UIProgressView в качестве подпредставления объекта UITextField.

UIProgressView* progressView = [[UIProgressView alloc] init];
[aUITextField addSubview:progressView];
progressView.progress = 0.5;
[progressView release];

2. Подкласс UITextfield и переопределение drawRect:.

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor orangeColor] setFill];
    [[UIBezierPath bezierPathWithOvalInRect:rect] fill];
}

Оба подхода не сработали. Видите ли вы какие-либо проблемы с этими подходами? И как я могу сделать эту работу?

Ответы [ 3 ]

2 голосов
/ 11 июня 2011

Я не уверен, что добавление UIProgressView в качестве подпредставления объекта UITextField будет полезно, так как вы не можете изменить рамку просмотра прогресса.

Подклассификация кажется правильным подходом.Вот что я мог придумать.Проверьте, полезно ли это вам.

ProgressField.h

@interface ProgressField : UITextField {

}

@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, retain) UIColor * progressColor;

@end

ProgressField.m

@implementation ProgressField
@synthesize progress;
@synthesize progressColor;

- (void)setProgress:(CGFloat)aProgress {
    if ( aProgress < 0.0 || aProgress > 1.0 ) {
        return;
    }

    progress = aProgress;

    CGRect progressRect = CGRectZero;
    CGSize progressSize = CGSizeMake(progress * CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
    progressRect.size = progressSize;

    // Create the background image
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, self.bounds);

    CGContextSetFillColorWithColor(context, [self progressColor].CGColor);
    CGContextFillRect(context, progressRect);

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [super setBackground:image];
}

- (void)setBackground:(UIImage *)background {
    // NO-OP
}

- (UIImage *)background {
    return nil;
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setBorderStyle:UITextBorderStyleBezel];
    }
    return self;
}

Похоже, что это не работает с UITextField с borderStyle, установленным на UITextBorderStyleRoundedRect.

1 голос
/ 11 июня 2011
UIProgressView* progressView = [[UIProgressView alloc] init];
progressView.frame = aUITextField.frame;// you can give even set the frame of your own using CGRectMake();
[aUITextField addSubview:progressView];
progressView.progress = 0.5;
[progressView release];

Установить кадр прогресса просмотра.

0 голосов
/ 11 июня 2011

Здесь я думаю, что вам нужно добавить progressView в качестве подпредставления для self.view, просто установить фрейм progressView в соответствии с размером, который будет соответствовать UITextField, и сделать центр обзора в центре UITextField.надеюсь, это поможет вам.

...