2016-03-06 98 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方法的代碼。