简体   繁体   English

配置 VS Code 的 inlay-hints 的字体样式

[英]Configuring the font-style of VS Code's inlay-hints

How can I change the font's style for VS Code's "Inlay Hints" feature?如何更改 VS Code 的“嵌入提示”功能的字体样式?

I attempted to search for an answer, but could only find how to change the color, not the style.我试图寻找答案,但只能找到如何更改颜色,而不是样式。

Here's an example:这是一个例子:

I would like the word target in the image below to render italic .我希望下图中的target词呈现为斜体

例如我希望目标是斜体


I tried the following, however, it did not work...我尝试了以下,但是,它没有用......

 {
      scope: "inlayHint",
      settings: {
        fontStyle: "italic",
      },
    },

I also tried adding editor.inlayHint to the scope, but the attempt was without success.我也尝试将editor.inlayHint添加到 scope,但没有成功。


Setting the Font-style to italic isn't Possible.无法将字体样式设置为italic

Not all is lost though, there's a workaround that actually works pretty well.不过,并非所有内容都丢失了,有一种解决方法实际上效果很好。

As I stated above, setting the font-style to italic is not possible.如上所述,无法将字体样式设置为斜体。 There is, however, a workaround, and it works pretty darn well.但是,有一种解决方法,而且效果非常好。 Before I explain the workaround, I think its important to note that the first solution I tried to use, was to set the font to an italic variant, unfortunately setting "editor.inlayHints.fontFamily" to an italic variant of a font was something that VS Code didn't seem to allow.在我解释解决方法之前,我认为重要的是要注意我尝试使用的第一个解决方案是将字体设置为斜体变体,不幸的是,将"editor.inlayHints.fontFamily"设置为字体的斜体变体是这样的VS Code 似乎不允许。 I have seen this in many other situations.我在许多其他情况下都看到了这一点。 The idea is, since you can't do this "fontStyle": "italic" , you just change the font to an italic version.这个想法是,因为你不能这样做"fontStyle": "italic" ,你只需将字体更改为斜体版本。

For example:例如:

If I use Cascadia Code as my font, then — in theory — I can just us the following configuration:如果我使用Cascadia Code作为我的字体,那么——理论上——我可以只使用以下配置:
// @file "settings.json"

{
    "editor.inlayHints.fontFamily": "'Cascadia Code Italic'"
}

...however, it unfortunately doesn't work that way — or at-least not in this situation . ...但是,不幸的是,它不能那样工作——或者至少在这种情况下不能 Not only do italic varients not work, but neither do bold variants and/or expanded variants (ie 'Inconsolata SemiExpanded', 'Inconsolata Bold', etc...) .不仅斜体变体不起作用,粗体变体和/或扩展变体(即“Inconsolata SemiExpanded”、“Inconsolata Bold”等)也不起作用


While moving forward, I will state the obvious:在前进的同时,我将 state 显而易见:

There is no "editor.inlayHints.fontStyle" setting.没有"editor.inlayHints.fontStyle"设置。


So what do we do?那么我们该怎么办?

Well we can set the font-family using "editor.inlayHints.fontFamily" .好吧,我们可以使用"editor.inlayHints.fontFamily"设置字体系列。

Its a bit suprising that we can set the font, but not the font's style.我们可以设置字体,但不能设置字体的样式,这有点令人惊讶。 I am not sure why they designed it this way, but it's obvious that they built the feature with specific constraints for reasons that are beyond me (reading the feature's development feed on GitHub would probably help anyone who wanted to know more about why the feature works how it does).我不确定他们为什么以这种方式设计它,但很明显,他们构建了具有特定约束的功能,原因超出了我的范围(阅读 GitHub 上的功能开发提要可能会帮助任何想了解更多关于该功能为何起作用的人它是怎么做的)。 Nevertheless, we have a highly customizable setting, that actually gives us far more control than the setting that we established, does not exist , editor.inlayHints.font .尽管如此,我们有一个高度可定制的设置,它实际上比我们建立的设置(不存在editor.inlayHints.font给了我们更多的控制权。

What I do, personally, which has a cool tricked-out look, is I find a non-monospaced hand-written style of font — usually from Google Fonts — and I assign that to the font-family.我个人所做的,看起来很酷的,是我找到了一种非等宽的手写字体样式——通常来自谷歌 Fonts——我将它分配给字体系列。 It took me a while to find some fonts that work.我花了一段时间才找到一些有效的 fonts。 Anyone who has ever played with fonts, trying to find which font works best for them to write code with, will know: Picking fonts that are used anywhere inside the editor (or inside your code) is going to require great scrutiny, and the longer you code, the more set in your ways you get, so this can actually be a time consuming setting to configure.任何曾经使用过 fonts 并试图找到最适合他们编写代码的字体的人都会知道:选择在编辑器内(或代码内)任何地方使用的 fonts 都需要仔细检查,而且需要更长的时间您编写的代码越多,您获得的设置就越多,因此这实际上可能是一个耗时的配置设置。 It took me an hour to settle on a font, and I settled on using the "Sriracha" font-family.我花了一个小时来确定一种字体,然后我决定使用“Sriracha”字体系列。 Siracha is oblique in appearance (looks italic), its readable in the editor, it renders well in the editor (which not all fonts are going to do), and its slightly bolder than the standard weight of the font I use wich is Cascadia Code. Siracha 在外观上是倾斜的(看起来是斜体),它在编辑器中可读,它在编辑器中呈现良好(并非所有 fonts 都会这样做),并且它比我使用的字体的标准粗细略粗,即 Cascadia Code .


My Configuration results in the following appearance:我的配置导致以下外观:

Inlay Hints 更改字体样式的解决方案



When changing the font family, there are a couple other settings that help to configure this feature.更改字体系列时,还有一些其他设置有助于配置此功能。


So, in other words, just do the following:因此,换句话说,只需执行以下操作:
  1. Download the italic &/or bold style font you wont from Google Fonts , Font Squirrel , or some other font source you perfer.Google FontsFont Squirrel或其他您喜欢的字体源下载斜体和/或粗体字体。

  2. Set the font family using "editor.inlayHints.fontFamily" .使用"editor.inlayHints.fontFamily"设置字体系列。

  3. Critique the fonts configuration using the other settings available below.使用下面可用的其他设置来评价 fonts 配置。

// @file "settings.json"

{
    // Font Family
    "editor.inlayHints.fontFamily": "/*NAME OF FONT FAMILY*/",

    // Font Size
    "editor.inlayHints.fontSize": 14, // <-- Set the font-size you want
    // Theme Colors Override
    "workbench.colorCustomizations": {
        // Overrides Theme Default Colors for InlayHints feature 
        "editorInlayHint.background": "#00001CCC",
        "editorInlayHint.foreground": "#99FFBBCC",

        // Overrides Theme Parameter hints fg for InlayHints feature
        "editorInlayHint.parameterBackground": "#00001CCC",
        "editorInlayHint.parameterForeground": "#99FFBBCC",

        // Overrides Theme Type hints fg for InlayHints feature
        "editorInlayHint.typeBackground": "#08000088",
        "editorInlayHint.typeForeground": "#DDEEFF88"
    },
}

Really its quite simple.真的很简单。

By being able to set the inlayHints font family to anything we want, we can achieve any sort of font style we want.通过能够将 inlayHints 字体系列设置为我们想要的任何字体,我们可以实现我们想要的任何字体样式。

In settings.json you need a line likesettings.json你需要一行

 "editor.inlayHints.fontFamily": "'Myfontitalic'",

Now, it seems vscode has some trouble reading a font name with non alphanumeric characters, if for instance the italic version is "Myfont-Italic" or "Myfont Italic".现在,似乎vscode在读取带有非字母数字字符的字体名称时遇到了一些麻烦,例如,如果斜体版本是“Myfont-Italic”或“Myfont Italic”。

A solution is to copy and rename the font to a new name, like "Myfontitalic", without spaces or other characters.一种解决方案是复制字体并将其重命名为新名称,如“Myfontitalic”,不带空格或其他字符。

What I did was for Ubuntu but the same idea should work on any Linux, and probably Windows, using other tools (fontforge?)我所做的是针对 Ubuntu 但同样的想法应该适用于任何 Linux,可能还有 Windows,使用其他工具(fontforge?)

  • apt install fonttools font-manager to get ttx a utility to change font info (font-manager is also useful) apt install fonttools font-manager以获取ttx更改字体信息的实用程序(font-manager 也很有用)
  • use a search, font-manager, font-viewer, ... to find the location of the font (usually /usr/share/fonts/... or ~/.local/share/fonts )使用搜索、字体管理器、字体查看器...来查找字体的位置(通常是/usr/share/fonts/...~/.local/share/fonts
  • copy a fresh version somewhere (home), cp ~/.local/share/fonts/Myfont-Italic.otf ~/temp.otf and cd to go home在某处(家)复制一个新版本, cp ~/.local/share/fonts/Myfont-Italic.otf ~/temp.otfcd到 go 家
  • translate to an XML copy ttx temp.otf as temp.ttx转换为 XML 将ttx temp.otf复制为temp.ttx
  • edit temp.ttx ( vi , ...), and change all "Myfont-Italic", "Myfont Italic"... (relevant names) to a unique "Myfontitalic" name, save.编辑temp.ttx ( vi , ...),并将所有“Myfont-Italic”、“Myfont Italic”...(相关名称)更改为唯一的“Myfontitalic”名称,保存。
  • translate back to otf (...) with ttx temp.ttx (creates Myfontitalic.otf if it was an otf file)使用ttx temp.ttx转换回 otf (...)(如果它是 otf 文件则创建Myfontitalic.otf

To install the new font (locally), either double-click on it in the file explorer ( ~/Myfontitalic.otf ) or use a tool.要(本地)安装新字体,请在文件资源管理器 ( ~/Myfontitalic.otf ) 中双击它或使用工具。 Restart font-manager , the new font should be visible.重新启动font-manager ,新字体应该是可见的。

Restart vscode and add the line above in settings.json .重新启动vscode并在settings.json中添加上面的行。

在此处输入图像描述

on a Mac find the font variant name like this:在 Mac 上找到这样的字体变体名称:

  1. Open "Font Book" App打开“字体书”应用程序
  2. Expand the desired font and select a variant.展开所需的字体和 select 变体。
  3. Click the (i) icon on the Font Book toolbar.单击字体册工具栏上的 (i) 图标。
  4. Use the PostScript Name name in VS Code - for example "editor.inlayHints.fontFamily": "DankMono-Italic"在 VS Code 中使用PostScript Name名称——例如"editor.inlayHints.fontFamily": "DankMono-Italic"

No one tells you where to edit this and that makes it a pain in the butt.没有人告诉您在哪里编辑它,这让您很头疼。

File>Preferences>Settings, search for color customizations, click "Edit in settings.json" File>Preferences>Settings,搜索color customizations,点击“Edit in settings.json”

{
    "workbench.colorCustomizations": {
      "editorInlayHint.background": "#00000000",
      "editorInlayHint.foreground": "#666666FF",
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM