如何从Bootswatch的Lux主题提取字体并在R Shiny中用bslib使用
从Bootswatch Lux主题提取字体并应用到其他bslib主题
首先明确Bootswatch Lux主题的默认字体配置:
- 基础文本字体:
'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif - 等宽/代码字体:
SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace
以下是两种将这些字体应用到其他主题的实现方式:
方法1:使用bs_add_variables直接覆盖字体变量
这种方式可以精确控制所有字体相关的Bootstrap变量,适合需要同时设置基础字体和代码字体的场景:
library(bslib) # 基于目标主题创建自定义主题,替换为Lux的字体 custom_theme <- bs_theme( version = 5, bootswatch = "cerulean" # 替换成你需要的目标主题名称 ) |> bs_add_variables( "font-family-base" = "'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif", "font-family-monospace" = "SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace" )
方法2:使用base_font参数快速设置基础字体
如果只需要替换基础文本字体,bs_theme的base_font参数是更简洁的选择:
library(bslib) # 方式A:直接指定字体字符串(依赖系统已安装对应字体) custom_theme <- bs_theme( version = 5, bootswatch = "cerulean", base_font = "'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif" ) # 方式B:通过Google Fonts加载字体(适合系统未安装Segoe UI的场景) custom_theme <- bs_theme( version = 5, bootswatch = "cerulean", base_font = font_google("Segoe UI", local = FALSE) )
补充说明
Lux主题的标题字体与基础字体一致,无需额外设置font-family-heading变量;如果目标主题有特殊的标题字体需求,也可以通过bs_add_variables添加该变量进行覆盖。
内容的提问来源于stack exchange,提问作者rtb
相关产品推荐
相关产品推荐

