2012-04-08 15 views

回答

4

我想你可以用这个作为初始的指导,然后构建它:

How to draw an oval speech bubble programmatically on iPhone?

下面是从后samfu_1的回答

我会做在两次迭代中。首先获取上下文并开始一个 路径。填充一个椭圆,然后用三行包围三角形 的自定义路径。我假定以下尺寸:70宽度,62高度。倍率绘制矩形中的UIView的子类,在 实例化一个子类的UIViewController:

-(void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(ctx, 0.0, 0.0, 1.0, 1.0); 
    CGContextFillEllipseInRect(ctx, CGRectMake(0.0, 0.0, 70.0, 50.0)); //oval shape 
    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, 8.0, 40.0); 
    CGContextAddLineToPoint(ctx, 6.0, 50.0); 
    CGContextAddLineToPoint(ctx, 18.0, 45.0); 
    CGContextClosePath(ctx); 
    CGContextFillPath(ctx); 
} 

生成此在当针对一个灰 背景添加的iPhone模拟器:

enter image description here

这第二代码例如将几乎重复你上面生产的 。我使用灵活的尺寸实现了这个功能,当你实例化它时,它可以提供给UIView框架的 。本质上讲,白色 的讲话泡泡部分是用黑色笔画绘制而成的,后面跟着 。

-(void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGRect aRect = CGRectMake(2.0, 2.0, (self.bounds.size.width * 0.95f), (self.bounds.size.width * 0.60f)); // set the rect with inset. 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); //white fill 
    CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0); //black stroke 
    CGContextSetLineWidth(ctx, 2.0); 


    CGContextFillEllipseInRect(ctx, aRect); 
    CGContextStrokeEllipseInRect(ctx, aRect);  

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, (self.bounds.size.width * 0.10), (self.bounds.size.width * 0.48f)); 
    CGContextAddLineToPoint(ctx, 3.0, (self.bounds.size.height *0.80f)); 
    CGContextAddLineToPoint(ctx, 20.0, (self.bounds.size.height *0.70f)); 
    CGContextClosePath(ctx); 
    CGContextFillPath(ctx); 

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, (self.bounds.size.width * 0.10), (self.bounds.size.width * 0.48f)); 
    CGContextAddLineToPoint(ctx, 3.0, (self.bounds.size.height *0.80f)); 
    CGContextStrokePath(ctx); 

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, 3.0, (self.bounds.size.height *0.80f)); 
    CGContextAddLineToPoint(ctx, 20.0, (self.bounds.size.height *0.70f)); 
    CGContextStrokePath(ctx); 
} 

enter image description here

编辑:

您也可以参考布拉德·拉尔森的回答。在这个岗位

How to draw a "speech bubble" on an iPhone?

希望这可以帮助你。

让我知道你是否需要更多帮助。

+0

恩......我想根据文本的长度来改变spb的大小,但我不知道如何制作它.. – tulurira 2012-04-20 04:11:41