iOS实现气泡弹出并显示文字

直接上代码,你的得偿所愿:

- (void)showBubbleWithPoint:(CGPoint)thePoint text:(NSString*)text borderColor:(UIColor*)borderColor textColor:(UIColor*)textColor isMain:(BOOL)isMain {

if(isMain) {

for(CALayer*layerinself.mainBubbleLayerArray) {

[layerremoveFromSuperlayer];

}

}else{

for(CALayer*layerinself.bubbleLayerArray) {

[layerremoveFromSuperlayer];

}

}

CGFloatsuggestHeight = [MMUtilscalTextRectWithString:textlineSpacing:3fontSize:10boundingRectSize:CGSizeMake(kValueBetween,CGFLOAT_MAX)].size.height;

CGPointpoint =CGPointZero;

if(thePoint.y< (suggestHeight +3)) {

point =CGPointMake(thePoint.x, suggestHeight +3);

}else{

point =CGPointMake(thePoint.x, thePoint.y);

}

UIBezierPath*path = [UIBezierPathbezierPath];

[pathmoveToPoint:point];

[pathaddLineToPoint:CGPointMake(point.x-3, point.y-3)];

[pathaddLineToPoint:CGPointMake(point.x-(kValueBetween/2)+3, point.y-3)];

[pathaddArcWithCenter:CGPointMake(point.x-(kValueBetween/2)+3, point.y-6)radius:3startAngle:0.5*M_PIendAngle:1*M_PIclockwise:YES];

[pathaddLineToPoint:CGPointMake(point.x- (kValueBetween/2), point.y- suggestHeight)];

[pathaddArcWithCenter:CGPointMake(point.x-(kValueBetween/2)+3, point.y- suggestHeight)radius:3startAngle:1*M_PIendAngle:1.5*M_PIclockwise:YES];

[pathaddLineToPoint:CGPointMake(point.x+(kValueBetween/2)-3, point.y- suggestHeight -3)];

[pathaddArcWithCenter:CGPointMake(point.x+(kValueBetween/2)-3, point.y- suggestHeight)radius:3startAngle:1.5*M_PIendAngle:2*M_PIclockwise:YES];

[pathaddLineToPoint:CGPointMake(point.x+(kValueBetween/2), point.y-6)];

[pathaddArcWithCenter:CGPointMake(point.x+(kValueBetween/2)-3, point.y-6)radius:3startAngle:0*M_PIendAngle:0.5*M_PIclockwise:YES];

[pathaddLineToPoint:CGPointMake(point.x+3, point.y-3)];

[pathaddLineToPoint:CGPointMake(point.x, point.y)];

CAShapeLayer*shapeLayer = [CAShapeLayerlayer];

shapeLayer.strokeColor= borderColor.CGColor;

shapeLayer.fillColor= [UIColorwhiteColor].CGColor;

shapeLayer.lineWidth=1;

shapeLayer.lineJoin=kCALineJoinRound;

shapeLayer.lineCap=kCALineCapRound;

shapeLayer.path= path.CGPath;

shapeLayer.zPosition=1;

[self.bgView.layeraddSublayer:shapeLayer];

if(isMain) {

[self.mainBubbleLayerArrayaddObject:shapeLayer];

}else{

[self.bubbleLayerArrayaddObject:shapeLayer];

}

CATextLayer*txtLayer = [CATextLayerlayer];

txtLayer.string= text;

txtLayer.frame=CGRectMake(point.x-(kValueBetween/2), point.y- suggestHeight -3,kValueBetween, suggestHeight);

txtLayer.backgroundColor= [UIColorclearColor].CGColor;

txtLayer.foregroundColor= textColor.CGColor;

txtLayer.fontSize=10;

txtLayer.alignmentMode=kCAAlignmentCenter;

txtLayer.contentsScale= [UIScreenmainScreen].scale;

txtLayer.zPosition=1;

[self.bgView.layeraddSublayer:txtLayer];

if(isMain) {

[self.mainBubbleLayerArrayaddObject:txtLayer];

}else{

[self.bubbleLayerArrayaddObject:txtLayer];

}

}

推荐阅读更多精彩内容