Class FontBuilder
java.lang.Object
io.leandev.appfuse.font.FontBuilder
字體建構器,提供流暢的 API 來建立和配置字體。
支援兩種字體來源:
- 系統字體:使用
newFont(String)或newFont(Locale) - 自訂字體檔案:使用
newFont(InputStream)載入應用提供的字體
範例用法:
// 使用系統字體
Font font1 = FontBuilder.newFont(Font.SANS_SERIF)
.withStyle(Font.BOLD)
.ofSize(14)
.build();
// 使用 Locale 選擇適合的系統字體
Font font2 = FontBuilder.newFont(Locale.JAPANESE)
.ofSize(12)
.build();
// 載入應用層提供的自訂字體檔案
InputStream fontStream = getClass().getResourceAsStream("fonts/MyFont.ttf");
Font font3 = FontBuilder.newFont(fontStream)
.withStyle(Font.ITALIC)
.ofSize(16)
.build();
-
Method Summary
Modifier and TypeMethodDescriptionbuild()建構並註冊字體到系統。static FontBuildernewFont(FontFamily fontFamily) 使用字體家族枚舉建立字體建構器(使用系統字體)。static FontBuilder直接使用 Font 物件建立字體建構器。static FontBuildernewFont(InputStream inputStream) 從輸入串流載入字體檔案建立字體建構器。static FontBuilder使用系統字體名稱建立字體建構器。static FontBuilder根據 Locale 選擇適合的系統字體。ofSize(int size) 設定字體大小。withStyle(int style) 設定字體樣式。
-
Method Details
-
newFont
使用系統字體名稱建立字體建構器。- Parameters:
fontName- 字體名稱(如 Font.SANS_SERIF, Font.SERIF, "Arial" 等)- Returns:
- FontBuilder 實例
-
newFont
使用字體家族枚舉建立字體建構器(使用系統字體)。- Parameters:
fontFamily- 字體家族枚舉- Returns:
- FontBuilder 實例
-
newFont
從輸入串流載入字體檔案建立字體建構器。
應用層需自行提供字體檔案的 InputStream。 若載入失敗,將降級使用系統預設 SANS_SERIF 字體。
- Parameters:
inputStream- 字體檔案的輸入串流- Returns:
- FontBuilder 實例
-
newFont
根據 Locale 選擇適合的系統字體。
對於 CJK 語系,會優先選擇 Dialog 字體(較好的多語言支援); 其他語系使用 SANS_SERIF 字體。
- Parameters:
locale- 語言環境- Returns:
- FontBuilder 實例
-
newFont
直接使用 Font 物件建立字體建構器。- Parameters:
font- Font 物件- Returns:
- FontBuilder 實例
-
withStyle
設定字體樣式。- Parameters:
style- 字體樣式(Font.PLAIN, Font.BOLD, Font.ITALIC 或其組合)- Returns:
- FontBuilder 實例(供鏈式呼叫)
-
ofSize
設定字體大小。- Parameters:
size- 字體大小(點數)- Returns:
- FontBuilder 實例(供鏈式呼叫)
-
build
-