我正在使用delphi,我有一个文本,我想把它放到一个cricle;通过我想减少文本的字体以保持它在圈内,我知道如何保持它的代码一个矩形,但我;我很少与数学函数混淆,让我把它保持在圆圈内 这是我从网上冲浪的矩形代码
double fontSize = 20.0;
bool bFontFits = false;
while (bFontFits == false)
{
m_pCanvas->Font->Size = (int)fontSize;
TSize te = m_pCanvas->TextExtent(m_name.c_str());
if (te.cx < (width*0.90)) // Allow a little room on each side
{
// Calculate the position
m_labelOrigin.x = rectX + (width/2.0) - (te.cx/2);
m_labelOrigin.y = rectY + (height/2.0) - te.cy/2);
m_fontSize = fontSize;
bFontFits = true;
break;
}
fontSize -= 1.0;
}
答案 0 :(得分:2)
我会调整像这样的矩形的代码:
procedure CalcFontSizeRectangle(aCanvas : TCanvas; const aText : string; const aRect : TRect);
var
te : TSize;
begin
aCanvas.Font.Size := 20;
while aCanvas.Font.Size > 0 do begin
te := aCanvas.TextExtent(aText);
if (te.cx < ((aRect.Right-aRect.Left)*0.90)) and (te.cy < ((aRect.Bottom-aRect.Top)*0.90)) then begin
break;
end;
aCanvas.Font.Size := aCanvas.Font.Size - 1;
end;
end;
并稍微更改一下,让它适用于这样的圈子:
procedure CalcFontSizeCircle(aCanvas : TCanvas; const aText : string; const aDiameter : integer);
var
te : TSize;
d : double;
begin
aCanvas.Font.Size := 20;
while aCanvas.Font.Size > 0 do begin
te := aCanvas.TextExtent(aText);
d := sqrt(te.cx * te.cx + te.cy * te.cy);
if d < (aDiameter*0.90) then begin
break;
end;
aCanvas.Font.Size := aCanvas.Font.Size - 1;
end;
end;
结果字体大小在画布中。