在ios中绘制字母E

在ios中绘制字母E,ios,calayer,bezier,quartz-core,Ios,Calayer,Bezier,Quartz Core,如何使用beizer路径或其他方法在ios中绘制字母表E 我尝试使用E的普通图像,但它随屏幕分辨率的不同而变化,但不应变化,因此我将通过编码进行绘制。您可以创建自定义视图。(UIView的子类)。然后像这样改变它的draw rect方法 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef context = UIGraphicsGetCurrentContext(); CGCont

如何使用beizer路径或其他方法在ios中绘制字母表E


我尝试使用E的普通图像,但它随屏幕分辨率的不同而变化,但不应变化,因此我将通过编码进行绘制。

您可以创建自定义视图。(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);
}

:谢谢。它工作:)很好。但是如何使它成为视图的中心呢?我编辑了答案。希望对你有帮助。工作起来很有魅力@calgar:我需要E的中心水平线也是相同的长度,但是如果我在“定义中心水平线40”中增加长度,则表示该线不可见。你能帮我解决这个问题吗?我再次尝试使用(垂直水平线80,顶部水平线40,中心水平线40,底部水平线40)这些值,但你的情况并没有出现,我是说它正在发挥作用。你有什么改变吗?