【问题标题】:Drawing alphabet E in ios [closed]在ios中绘制字母E [关闭]
【发布时间】:2013-09-27 11:13:24
【问题描述】:

如何在ios中使用beizer路径之类的绘制字母E?

我尝试使用 E 的普通图像,但它会根据屏幕分辨率而变化,但它应该不会变化,所以我将通过编码进行绘制。

【问题讨论】:

    标签: ios calayer bezier quartz-core


    【解决方案1】:

    您可以创建自定义视图。 (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 的中心水平线的长度也相同,但是如果我增加 #define CENTER_HORIZONTAL_LINE 40 中的长度意味着,那条线不可见..你能帮我吗解决这个问题
    • 我再次尝试使用 (VERTICAL_LINE 80,TOP_HORIZONTAL_LINE 40, CENTER_HORIZONTAL_LINE 40,BOTTOM_HORIZONTAL_LINE 40 ) 这些值,但您的情况没有出现。我的意思是它正在工作。你有什么改变吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多