Я пытаюсь сделать индикатор с надписью в нем.Вот что я сделал до сих пор.
Вот код.
//PDProgressView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface PDProgressView : UIView {
CGFloat _progress;
}
@property (nonatomic) CGFloat progress;
-(id)initWithFrame:(CGRect)frame;
-(void)drawRect:(CGRect)rect;
@end
NS_ASSUME_NONNULL_END
//PDProgressView.m
#import "PDProgressView.h"
@implementation PDProgressView
@synthesize progress = _progress;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//init
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// Set up environment.
CGSize size = [self bounds].size;
UIColor *backgroundColor = [UIColor greenColor];
UIColor *foregroundColor = [UIColor whiteColor];
UIFont *font = [UIFont boldSystemFontOfSize:42.0];
// Prepare progress as a string.
NSString *progress = [NSString stringWithFormat:@"%d", (int)round([self progress] * 100)];
NSMutableDictionary *attributes = [@{ NSFontAttributeName : font } mutableCopy];
CGSize textSize = [progress sizeWithAttributes:attributes];
CGFloat progressX = ceil([self progress] * size.width);
CGPoint textPoint = CGPointMake(ceil((size.width - textSize.width) / 2.0), ceil((size.height - textSize.height) / 2.0));
// Draw background + foreground text
[backgroundColor setFill];
CGContextFillRect(context, [self bounds]);
attributes[NSForegroundColorAttributeName] = foregroundColor;
[progress drawAtPoint:textPoint withAttributes:attributes];
// Clip the drawing that follows to the remaining progress' frame.
CGContextSaveGState(context);
CGRect remainingProgressRect = CGRectMake(progressX, 0.0, size.width - progressX, size.height);
CGContextAddRect(context, remainingProgressRect);
CGContextClip(context);
// Draw again with inverted colors.
[foregroundColor setFill];
CGContextFillRect(context, [self bounds]);
attributes[NSForegroundColorAttributeName] = backgroundColor;
[progress drawAtPoint:textPoint withAttributes:attributes];
CGContextRestoreGState(context);
}
- (void)setProgress:(CGFloat)progress {
_progress = fminf(1.0, fmaxf(progress, 0.0));
[self setNeedsDisplay];
}
@end
Это коды от этого ответа .
Я хочу сделать белый цвет прозрачным.
Вот так (я только что переключил белый цвет на тот же, что и цвет фона)
Итак, я попытался изменить foregroundColor
на clearColor
.Но я получил прямоугольник с greenColor
.
Что я должен сделать, чтобы сделать белый цвет прозрачным, как второе изображение?