2016-03-06 120 views
0

我想画一个E形thingy,据我所知有2种方法去其中之一是路径。画一个形状

我可以用下面的文档建议的路径绘制。 https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/BezierPaths/BezierPaths.html

或者我可以用UIView创建一个矩形,并用2个小方块雕刻它,然后通过减去这2个点来制作一个E。

我不确定哪种方式是考虑效率和一切的方式。如果还有其他更好的方法,请赐教。谢谢!

(我发现很多关于这里绘制形状,但没有一个甚至远程最近,我想最近的答案)

回答

1

一个的几个可能的方式:

  1. 创建的子类UIView
  2. 设置CAShapeLayer作为您的UIView的支持层。
  3. 通过添加线条来反映您的E形状来配置UIBezierPath。
  4. 将UIBezierPath CGPath属性分配给CAShapeLayer的path属性。
  5. 添加您的视图以查看层次结构。

其他:

  1. 创建的UIView一个子类。
  2. 重写drawRect方法。
  3. 将图形/线条添加到绘图上下文以反映您的E形状。
  4. 添加您的视图以查看层次结构。

恕我直言第一个解决方案会更有效率,但我通常会进入第二个。

0

如果你不想使用贝塞尔路径,你可以借鉴的信上的观点:

CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Text Drawing 
    CGRect textRect = CGRectMake(CGRectGetMinX(frame) + 52, CGRectGetMinY(frame) + 26, 17, 21); 
    { 
     NSString* textContent = @"E"; // You can draw any letter , just replace this! 
     NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy; 
     textStyle.alignment = NSTextAlignmentCenter; 

     NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize: UIFont.labelFontSize], NSForegroundColorAttributeName: UIColor.blackColor, NSParagraphStyleAttributeName: textStyle}; 

     CGFloat textTextHeight = [textContent boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY) options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height; 
     CGContextSaveGState(context); 
     CGContextClipToRect(context, textRect); 
     [textContent drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight)/2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes]; 
     CGContextRestoreGState(context); 
    } 

子类UIView的,包括在drawRect方法的代码。