You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在饼图(或饼图扇区中心)对齐文本?需在饼图中添加文本

如何将文本对齐到饼图扇区中心?

嘿,我来帮你搞定这个饼图文本居中的问题!其实核心就是先算出每个扇区的中心坐标,再让文本在这个坐标处居中绘制就行。结合你给出的代码,我给你一步步拆解实现:

步骤1:计算扇区的中心坐标

每个扇区的起始角度是arcOffset-(myAngle/2),结束角度是arcOffset+(myAngle/2),所以扇区的中心角度就是arcOffset(刚好是两个角度的平均值)。接下来用三角函数算出这个角度对应的坐标:

// 计算扇区中心的弧度角度(你的arcOffset应该已经是弧度制了,如果是角度要转成弧度:角度 * M_PI / 180)
CGFloat sectorCenterAngle = arcOffset;
// 控制文本在扇区中的位置,用半径的比例(比如0.6,避免文本贴到扇区边缘)
CGFloat textRadius = radius * 0.6;
// 计算文本中心的坐标
CGFloat textX = newCenterX + textRadius * cos(sectorCenterAngle);
CGFloat textY = newCenterY + textRadius * sin(sectorCenterAngle);

步骤2:准备文本与绘制属性

接下来定义你要显示的文本(比如百分比、标签),以及文本的样式:

// 替换成你实际要显示的文本内容,比如对应扇区的数值或标签
NSString *sectorText = [NSString stringWithFormat:@"%d%%", (int)(yourDataValue * 100)];

// 设置文本属性:字体、颜色等,可根据需求调整
NSDictionary *textAttributes = @{
    NSFontAttributeName: [UIFont boldSystemFontOfSize:14],
    NSForegroundColorAttributeName: [UIColor whiteColor]
};

步骤3:让文本居中对齐

因为drawAtPoint:是从文本的左上角开始绘制,所以我们需要计算文本的尺寸,把绘制原点调整到文本中心与扇区中心重合的位置:

// 计算文本的实际尺寸
CGSize textSize = [sectorText sizeWithAttributes:textAttributes];
// 调整绘制原点,让文本中心对齐到扇区中心
CGPoint drawOrigin = CGPointMake(textX - textSize.width/2, textY - textSize.height/2);

// 绘制文本
[sectorText drawAtPoint:drawOrigin withAttributes:textAttributes];

整合到你的现有代码里

把上面的代码片段插入到你绘制完每个扇区(CGContextFillPath(context);之后)的位置,完整的代码片段如下:

UIGraphicsPushContext(context); 
CGContextMoveToPoint(context, newCenterX, newCenterY); 
CGContextAddArc(context, newCenterX, newCenterY, radius, arcOffset-(myAngle/2), arcOffset+(myAngle/2), 0); 
UIColor *color = [PIECHART_COLORS objectAtIndex:i%6]; 
CGContextSetFillColorWithColor(context, color.CGColor); 
CGContextClosePath(context); 
CGContextFillPath(context); 

// --- 新增的文本绘制代码 ---
CGFloat sectorCenterAngle = arcOffset;
CGFloat textRadius = radius * 0.6;
CGFloat textX = newCenterX + textRadius * cos(sectorCenterAngle);
CGFloat textY = newCenterY + textRadius * sin(sectorCenterAngle);

NSString *sectorText = [NSString stringWithFormat:@"%d%%", (int)(yourDataPercentage)]; // 替换成你的数据
NSDictionary *textAttributes = @{
    NSFontAttributeName: [UIFont boldSystemFontOfSize:14],
    NSForegroundColorAttributeName: [UIColor whiteColor]
};

CGSize textSize = [sectorText sizeWithAttributes:textAttributes];
CGPoint drawOrigin = CGPointMake(textX - textSize.width/2, textY - textSize.height/2);
[sectorText drawAtPoint:drawOrigin withAttributes:textAttributes];
// --- 文本绘制代码结束 ---

CGMutablePathRef arcPath = CGPathCreateMutable(); 
CGPathAddArc(arcPath, NULL, self.frame.size.width/2, self.frame.size.height/2, ...);
// 后续代码...

一些实用小提示

  • 调整textRadius的数值:如果文本太靠近饼图边缘,就减小比例(比如0.5);如果太靠近中心,就增大(比如0.7)。
  • 文本可读性:如果扇区颜色较浅,记得把文本颜色改成深色(比如[UIColor blackColor]),避免看不清。
  • 角度方向:Core Graphics中0弧度是向右,角度顺时针递增。如果你的arcOffset是逆时针计算的,记得把sin的结果改成负的(textY = newCenterY - textRadius * sin(sectorCenterAngle))。

内容的提问来源于stack exchange,提问作者Dinesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:58:53