backgroundColor が UIColor カテゴリに設定されているオブジェクトの境界を取得する方法はありますか?
たとえば、画像から UIColor を適用しようとしていますが、それに応じて引き延ばしたいと考えています。関連付けられた参照は仕事をしますか? または、そのようなメソッドを UIView カテゴリに実装するのが最善でしょうか?
アップデート:
backgroundColor を設定するメソッドは次のとおりです。
+ (UIColor *)colorWithGradientStyle:(GradientStyle)gradientStyle andColors:(NSArray *)colors {
//Create our background gradient layer
CAGradientLayer *backgroundGradientLayer = [CAGradientLayer layer];
//Set the frame to our object's bounds
backgroundGradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
//To simplfy formatting, we'll iterate through our colors array and create a mutable array with their CG counterparts
NSMutableArray *cgColors = [[NSMutableArray alloc] init];
for (UIColor *color in colors) {
[cgColors addObject:(id)[color CGColor]];
}
switch (gradientStyle) {
case linearLeftToRight: {
//Set out gradient's colors
backgroundGradientLayer.colors = cgColors;
//Specify the direction our gradient will take
[backgroundGradientLayer setStartPoint:CGPointMake(0.0, 0.5)];
[backgroundGradientLayer setEndPoint:CGPointMake(1.0, 0.5)];
//Convert our CALayer to a UIImage object
UIGraphicsBeginImageContext(backgroundGradientLayer.bounds.size);
[backgroundGradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIColor colorWithPatternImage:backgroundColorImage];
}
case linearTopToBottom:
default: {
//Set out gradient's colors
backgroundGradientLayer.colors = cgColors;
//Convert our CALayer to a UIImage object
UIGraphicsBeginImageContext(backgroundGradientLayer.bounds.size);
[backgroundGradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * backgroundColorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIColor colorWithPatternImage:backgroundColorImage];
}
}
}