我正在制作一个基本上如下图形的图例:
[ ] Line 1
[ ] Line 2
[ ] Line 3
左侧的框需要与图表上的线条颜色相同。
无论如何,我需要知道的是,用Core Graphics绘制方框是否更快,或者只是用GIMP为方块制作一些png并包含它们。
答案 0 :(得分:7)
对每个图例使用UIView并将其背景颜色设置为您想要的颜色。
答案 1 :(得分:1)
这两种方法都足够快,不应该有所作为。但是,使用Core Graphics的优势在于您可以更加灵活,例如当你后来决定需要额外的颜色时。此外,您的应用程序将更小,因为您不必包含PNG文件。
答案 2 :(得分:0)
绘图框很快!我会每天使用Core Graphics,特别是因为你免费获得视网膜支持。
从这个例子中可以看出,你可以使用仅限UIKit的类来实现它:
// Setup colors
[myBoxColor setFill];
[myBoxBorderColor set];
// Setup a path for the box
UIBezierPath* path = [UIBezierBath bezierPathWithRect:rectOfTheBox];
path.lineWidth = 2;
// Draw!
[path fill];
[path stroke];
一个警告;笔划使用路径边缘作为线条的中心填充。因此,如果使用具有1点线宽的整数矩形描边,则会得到模糊的线。
你可以解决这个问题,你想通过这样的方式获得边界的1点线:
CGRect strokeRect = UIEdgeInsetsInsetRect(rectOfTheBox,
UIEdgeInsetsMake(0.5f,0.5f,0.5f,0.5f));
UIBezierPath* path = [UIBezierPath bezierPathWithRect:strokeRect];
[path stroke];
答案 3 :(得分:0)
在iOS上,Core Graphics使用起来非常简单。在您的视图的drawRect:
方法中,只需执行此操作即可绘制正方形:
- (void)drawRect:(CGRect)frame {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1); // gray
CGContextFillRect(context, CGRectMake(10, 10, 20, 20)); // our rect is {10,10,20,20)
// draw a line
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextBeginPath(context);
CGContextMoveToPoint(context, startX, startY);
CGContextAddLineToPoint(context, endX, endY);
CGContextStrokePath(context);
}
希望这有帮助!