0

beizerパスなどを使用してiosでアルファベットEを描画するにはどうすればよいですか?

Eは通常の画像で試してみたのですが、画面の解像度によって変わるのですが、変わらないはずなので、コーディングで描いてみます。

4

1 に答える 1

0

カスタム ビューを作成できます。( のサブクラスUIView)。そして、 draw rect メソッドを次のように変更します。

- (void)drawRect:(CGRect)rect {
     [super drawRect:rect];

     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

     CGContextSetLineWidth(context, 3.0);

     CGContextMoveToPoint(context, 0,0);

     CGContextAddLineToPoint(context, 0, 60);

     CGContextMoveToPoint(context, 0,0);

     CGContextAddLineToPoint(context, 30, 0);

     CGContextMoveToPoint(context, 0, 30);

     CGContextAddLineToPoint(context, 20, 30);

     CGContextMoveToPoint(context, 0, 60);

     CGContextAddLineToPoint(context, 30, 60);

     CGContextStrokePath(context);
}

必要に応じて、線の色、幅、またはその他のプロパティを変更できます。

編集: センターの場合、

最初に E 文字の幅を定義します。

#define VERTICAL_LINE 40
#define TOP_HORIZONTAL_LINE 40
#define CENTER_HORIZONTAL_LINE 40
#define BOTTOM_HORIZONTAL_LINE 40
#define LINE_PIECE 20

そしてdrawRectメソッド;

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

    CGContextSetLineWidth(context, 3.0);


    // assumed top and bottom lines are equal width

    CGFloat x, y;
    x = (self.frame.size.width - MAX(TOP_HORIZONTAL_LINE,CENTER_HORIZONTAL_LINE)) / 2;
    y = (self.frame.size.height - VERTICAL_LINE) / 2;

    CGContextMoveToPoint(context, x, y);

    CGContextAddLineToPoint(context, x, y + VERTICAL_LINE);

    CGContextMoveToPoint(context, x,y);

    CGContextAddLineToPoint(context, x+TOP_HORIZONTAL_LINE, y);

    CGContextMoveToPoint(context, x, y+LINE_PIECE);

    CGContextAddLineToPoint(context, x+CENTER_HORIZONTAL_LINE, y+LINE_PIECE);

    CGContextMoveToPoint(context, x, y+VERTICAL_LINE);

    CGContextAddLineToPoint(context, x + BOTTOM_HORIZONTAL_LINE, y + VERTICAL_LINE);

    // and now draw the Path!
    CGContextStrokePath(context);
}
于 2013-09-27T18:03:29.140 に答える