下面是將文本轉換爲那個被映射到多邊形紋理的另一種方法。這有拉伸文本以適應該區域的特徵
Show[Plot[x, {x, 0, 1}],
Graphics[{EdgeForm[Thick], Yellow, Rectangle[{.1, .5}, {.4, .9}]}],
Graphics[{Texture[ImageData[
Rasterize[Style["123", Red, Bold], "Image", RasterSize -> 300,
Background -> None]]],
Polygon[{{0.1, 0.5}, {0.4, 0.5}, {0.4, 0.9}, {0.1, 0.9}},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]}]]
![Mathematica graphics](https://i.stack.imgur.com/TGNat.png)
至於更容易比較的功能(因爲它不是真正的文字了。):
(* Render string/style s to fill a rectangle with left/bottom corner {l,b} and
right/top corner {r,t}. *)
textrect[s_, {{l_,b_},{r_,t_}}] := Graphics[{
Texture[ImageData[Rasterize[s, "Image", RasterSize->300, Background->None]]],
Polygon[{{l,b}, {r,b}, {r,t}, {l,t}},
VertexTextureCoordinates->{{0,0},{1,0},{1,1},{0,1}}]}]
不錯的做法。順便說一句,如果你使用Scaled [1]而不是'{Scaled [1],Scaled [1]}',文本將垂直居中。 – DavidC
@David謝謝。我試過了你的建議,但它不適用於我的系統。 : -/ –
添加窗格選項對齊 - >居中將使文本居中。 –