【问题标题】:Improper borders in draw rect绘制矩形中的边框不正确
【发布时间】:2013-03-27 17:50:52
【问题描述】:

我试图在 drawRect 方法中使用贝塞尔路径在矩形上绘制圆角,但不知何故,圆角显示在矩形的内侧,而不是内侧和外侧。代码如下。此处还附上当前正在绘制的边框(边框的外侧未圆角)

 - (void)drawRect:(CGRect)rect
 {


  // Drawing code
    CGContextRef context=UIGraphicsGetCurrentContext();

    //Set gray color to whole context
    [[UIColor lightGrayColor] set];
    CGContextSetAlpha(context,0.7);
    UIRectFill(rect);

    // Configure the context colors and line
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:131./255. green:148./255. blue:219./255. alpha:1.0].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 10.0);
    CGSize size=self.bounds.size;

    CGFloat radius = 10.0;

    CGSize guideSize=CGSizeMake(330, 130);
    CGRect guideCoords= CGRectMake(size.width*.5-guideSize.width*.5, size.height*.5-guideSize.height*.5, guideSize.width , guideSize.height);

    // Guide drawing
    CGContextStrokeRect(context,guideCoords);

    // Draw the Text
    [kVinGuideMessage drawInRect:CGRectMake(guideCoords.origin.x+kSideMessageMargin, guideCoords.origin.y+guideSize.height+kMarginFromGuide, guideCoords.size.width-2*kSideMessageMargin,40) withFont:[UIFont systemFontOfSize:16.0] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];


    //Get instersection and clear color of inner overlay
    CGRect holeRectIntersection = CGRectIntersection(rect,guideCoords);

    //----------ADDING ROUNDED CORNERS HERE-----------//
    CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:guideCoords cornerRadius:radius].CGPath;
    CGContextAddPath(context, clippath);
    CGContextClip(context);
    //------------------------------------------------//
    [[UIColor clearColor] setFill];
    UIRectFill(holeRectIntersection);

}

【问题讨论】:

    标签: iphone ios drawrect uibezierpath


    【解决方案1】:

    我认为外角是由CGContextStrokeRect(context,guideCoords); 绘制的。那时你还没有设置剪切路径,你的线宽是 10 点,那么为什么外角要圆角呢?我认为如果你在 guideCoords 矩形上调用 stroke rect 之前设置一个剪切路径(可能与底部的剪切路径不完全相同),你会有更好的运气。

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多