【发布时间】:2017-05-10 23:57:07
【问题描述】:
我们可以通过执行以下操作将 CGColor 设置为给定上下文的填充或描边颜色:
UIColor *color = [UIColor whiteColor];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, color.CGColor);
但是我们如何从给定的上下文中获取或检索当前设置的填充或描边颜色?
那么有没有类似的 API:
CGColorRef CGContextGetFillColorFromContext(CGContextRef c)
它将CGContext作为输入参数并返回它当前设置的填充或描边CGColour?如果不是这样,还有其他间接方法可以实现吗?
我希望以上是因为颜色是在一个类中设置的,并且在其他一些类中需要它:
在 MyView.m 中
- (void)drawRect:(CGRect)rect {
[[UIColor whiteColor] set]
//[@“Hello” drawInRect:rect withFont:titleFont lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentLeft]; //draws text in the rect with white font colour but removed becasue of API deprecation
[@“Hello” my_drawInRect:rect withFont:titleFont lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentLeft];
}
在 NSString+MyCategory.m 中
-(void)my_drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment {
NSMutableParagraphStyle* paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = lineBreakMode;
paragraphStyle.alignment = alignment;
UIColor* color = nil; //Hoping to get it from UIGraphicsGetCurrentContext();
[self drawInRect:rect withAttributes:@{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:color}];
}
【问题讨论】:
标签: ios objective-c macos core-graphics