在这里记录一下自己犯的一个很二的问题:
我的本意是写一个oldprice,中间一根删除线。至于用NSMutableAttributedString的方法这里不考虑了。
之前代码:
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 0, rect.size.height / 2);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextSetLineWidth(context, 1);
CGContextClosePath(context);
CGContextStrokePath(context);
}
运行之后,一直只有删除线,没有价钱,怎么看代码都没有错。
换种写法:
- (void)drawRect:(CGRect)rect{
// CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextMoveToPoint(context, 0, rect.size.height / 2);
// CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
// CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
// CGContextSetLineWidth(context, 1);
//
// CGContextClosePath(context);
// CGContextStrokePath(context);
UIBezierPath *be = [UIBezierPath bezierPath];
[be moveToPoint:CGPointMake(0, self.frame.size.height / 2)];
[be addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height / 2)];
[[UIColor redColor] setStroke];
[be closePath];
[be stroke];
}
还是一样,很无语。
结果出去接个水回来一看,nnd,忘了super。
- (void)drawRect:(CGRect)rect{
//一定不能忘记
[super drawRect: rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 0, rect.size.height / 2);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextSetLineWidth(context, 1);
CGContextClosePath(context);
CGContextStrokePath(context);
}
写在这里,提醒自己。