在 Xcode 4 中更改鼠标的 i-beam 光标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6738220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:31:17  来源:igfitidea点击:

Changing the mouse's i-beam cursor in Xcode 4

xcodemacoscolorsmouse-cursor

提问by phloopy

With a dark color scheme in Xcode 4 the i-beam cursor (aka text selection cursor) is nearly invisible. Is there a way to change the color of this cursor, either for Xcode specifically, or failing that, system-wide?

在 Xcode 4 中使用深色方案时,i 型光标(又名文本选择光标)几乎不可见。有没有办法改变这个光标的颜色,无论是专门针对 Xcode,还是在系统范围内失败?

It'd be nice if it would automatically change to a color contrasting its background too.

如果它也能自动更改为与其背景形成对比的颜色,那就太好了。

回答by Chris Dragon

As of Xcode 8, Apple has hidden the i-beam cursor somewhere where nobody has found it. Instead, many have chosen to use Mousecape to alter the i-beam in all programs. I prefer that method since it fixes the i-beam in other programs that support dark themes, such as Komodo editor. Mousecape should work in all Xcode versions and updating Xcode won't break the cursor. I'm currently using Mousecape in macOS 10.13.2, Xcode 9.1. Here are the steps:

从 Xcode 8 开始,Apple 将 i-beam 光标隐藏在没人发现的地方。相反,许多人选择使用 Mousecape 来改变所有程序中的 i-beam。我更喜欢这种方法,因为它修复了其他支持深色主题的程序(例如 Komodo 编辑器)中的 i-beam。Mousecape 应该适用于所有 Xcode 版本,更新 Xcode 不会破坏光标。我目前在 macOS 10.13.2、Xcode 9.1 中使用 Mousecape。以下是步骤:

  1. Download Mousecape from here
  2. Download a "cape" with modified i-beam cursor such as Bright whiteor Grey shadow
  3. Run Mousecape.app
  4. From Mousecape's menu bar: File > Import Cape > "cape" file downloaded above
  5. Right click imported "cape" and choose Apply
  6. Check the new cursor is working in Xcode. When satisfied, you can apply the cursor on each reboot by running a command from Mousecape's menu bar: Mousecape > Install Helper Tool
  7. Installing macOS updates (such as 10.13.1 to 10.13.2) may require repeating steps 5 and 6.
  1. 这里下载 Mousecape
  2. 下载带有修改过的 i-beam 光标的“斗篷”,例如亮白色灰色阴影
  3. Mousecape.app
  4. 从 Mousecape 的菜单栏中: File > Import Cape > "cape" file downloaded above
  5. 右键单击导入的“披风”并选择 Apply
  6. 检查新光标是否在 Xcode 中工作。满意后,您可以通过从 Mousecape 的菜单栏中运行命令来在每次重新启动时应用光标:Mousecape > Install Helper Tool
  7. 安装 macOS 更新(例如 10.13.1 到 10.13.2)可能需要重复步骤 5 和 6。

Original answer:

原答案:

Xcode does NOT use the system-wide i-beam cursor as everyone I found talking about it stated as if it was a known fact. If I hadn't believed those people, I wouldn't have spent two days figuring out how to alter the system i-beam cursor by editing CoreGraphicsonly to find that Xcode's ibeam doesn't change. BTW, I also stumbled on how to edit other system cursors.

Xcode 不使用系统范围的 i-beam 光标,因为我发现每个人都在谈论它,好像这是一个已知的事实。如果我不相信那些人,我就不会花两天时间弄清楚如何通过编辑 CoreGraphics来更改系统 i-beam 光标,结果却发现 Xcode 的 ibeam 没有改变。顺便说一句,我还偶然发现了如何编辑其他系统光标

I spent most of a weekend figuring this out, but the i-beam cursor in Xcode CAN be edited. It's simply a TIFF file in the following location for Xcode 3.2.6 (and earlier, I assume, but have not tested):

我花了一个周末的大部分时间来解决这个问题,但是可以编辑 Xcode 中的 i-beam 光标。它只是 Xcode 3.2.6 的以下位置中的一个 TIFF 文件(我假设更早,但尚未测试):

/Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/A/Resources/TIbeam.tiff

/Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/A/Resources/TIbeam.tiff

Xcode 4.1 has the same file but it does not affect the i-beam in the main editor (I assume it's used somewhere, but maybe not). Instead, the main editor in Xcode 4.1 uses this file:

Xcode 4.1 具有相同的文件,但它不会影响主编辑器中的 i-beam(我认为它在某处使用过,但可能没有)。相反,Xcode 4.1 中的主编辑器使用这个文件:

/Developer/Library/PrivateFrameworks/DVTKit.framework/Versions/A/Resources/DVTIbeamCursor.tiff

/Developer/Library/PrivateFrameworks/DVTKit.framework/Versions/A/Resources/DVTIbeamCursor.tiff

The ibeam files have moved again in Xcode 4.4:

ibeam 文件在 Xcode 4.4 中再次移动:

/Applications/Xcode.app/Contents/OtherFrameworks/XcodeEdit.framework/Versions/A/Resources/TIbeam.tiff

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/DVTIbeamCursor.png

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/[email protected]

/Applications/Xcode.app/Contents/OtherFrameworks/XcodeEdit.framework/Versions/A/Resources/TIbeam.tiff

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/DVTIbeamCursor.png

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/[email protected]

Editing DVTIbeamCursor.png was enough to change the i-beam in the main editor on my system. On another site, someone reported that [email protected] will be used on a retina display or Apple's other new high res displays like Thunderbolt and Cinema. Who knows if TIbeam.tiff is even used anymore since they didn't update it to png.

编辑 DVTIbeamCursor.png 足以在我系统的主编辑器中更改 i-beam。在另一个网站上,有人报告说 [email protected] 将用于视网膜显示器或 Apple 其他新的高分辨率显示器,如 Thunderbolt 和 Cinema。谁知道 TIbeam.tiff 是否已被使用,因为他们没有将其更新为 png。

According to comments below, in Xcode 5.0.2 all 3 cursor files were replaced with one file:

根据下面的评论,在 Xcode 5.0.2 中,所有 3 个光标文件都替换为一个文件:

/Applications/Xcode.app/Contents/OtherFrameworks/XcodeEdit.framework/Versions/A/Resources/DVTIbeamCursor.tiff

/Applications/Xcode.app/Contents/OtherFrameworks/XcodeEdit.framework/Versions/A/Resources/DVTIbeamCursor.tiff

And in 5.1.1 (boy, they sure love to change things in almost every version, don't they?):

在 5.1.1 中(男孩,他们肯定喜欢在几乎每个版本中更改内容,不是吗?):

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Re??sources/DVTIbeamCursor.tiff

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Re??sources/DVTIbeamCursor.tiff

I'm guessing that file is a double resolution (aka retina display) cursor that's scaled down for non-retina screens.

我猜这个文件是一个双分辨率(又名视网膜显示)光标,它为非视网膜屏幕缩小了。

If you can't find the ibeam file in your Xcode, try running this command in Terminal:

如果在 Xcode 中找不到 ibeam 文件,请尝试在终端中运行此命令:

sudo find / -name 'Ibeam' -print

sudo find / -name ' Ibeam' -print

In Xcode 7.3, the cursor was moved inside the following bundle file so the search command above won't find it:

在 Xcode 7.3 中,光标移动到以下包文件内,因此上面的搜索命令将找不到它:

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Resources/Assets.car

/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Resources/Assets.car

I recommend using a process found hereto patch a new cursor into the bundle file. If you don't like the cursor it installs (I felt it was still too dark and the grey outlines blend in with grey comment text), follow these steps:

我建议使用此处找到的过程将新游标修补到包文件中。如果您不喜欢它安装的光标(我觉得它仍然太暗,灰色轮廓与灰色注释文本融合在一起),请按照以下步骤操作:

  1. Download the latest release of Theme Engine(I successfully used version 1.0.0(111) with Xcode 7.3.1)
  2. Back up /Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/Assets.car
  3. Copy Assets.carsomewhere where it can be edited, such as to ~/Documents/Assets.car.
  4. Run Theme Engine.app, click Open Document, then open ~/Documents/Assets.car.
  5. Scroll down the list of items on the left and pick DVTIbeamCursor.
  6. You should see a normal resolution and double resolution ibeam cursor image in the center panel. Drag your own ibeam image on top of either cursor and it should change to show your image. I used a PNG image.
  7. Save, then close Theme Engine.
  8. Move your modified Assets.carback to /Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/Assets.carand keep a backup copy of your modified version somewhere so you don't have to do this again until a new Xcode version changes what's in Assets.car.
  1. 下载最新版本的主题引擎(我在 Xcode 7.3.1 中成功使用了 1.0.0(111) 版)
  2. 备份 /Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/Assets.car
  3. 复制Assets.car到可以编辑的地方,例如 to ~/Documents/Assets.car
  4. 运行Theme Engine.app,单击Open Document,然后打开~/Documents/Assets.car
  5. 向下滚动左侧的项目列表并选择DVTIbeamCursor
  6. 您应该会在中央面板中看到正常分辨率和双分辨率 ibeam 光标图像。将您自己的 ibeam 图像拖到任一光标的顶部,它应该会更改以显示您的图像。我使用了 PNG 图像。
  7. 保存,然后关闭主题引擎。
  8. 将修改Assets.car后的/Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/Assets.car版本移回并在某处保留修改版本的备份副本,这样您就不必再次执行此操作,直到新的 Xcode 版本更改Assets.car.

You may also wish to send an angry letter to Apple to complain that they have not fixed this problem in the last 7+ years and have instead made it progressively more difficult for users to patch Apple's broken cursor.

您可能还希望向 Apple 发送一封愤怒的信,抱怨他们在过去 7 年多的时间里没有解决这个问题,反而让用户修复 Apple 损坏的光标变得越来越困难。

Don't forget to keep backups of your edited cursors.Newer Xcode versions will overwrite your custom cursors with default ones when it does a software update.

不要忘记保留编辑过的光标的备份。较新的 Xcode 版本将在进行软件更新时用默认光标覆盖您的自定义光标。

Here's the ibeam cursor I use: In png formatRight click the ibeam and choose to save the image to get the png version, or click hereto download the tiff version.

这是我使用的 ibeam 光标:png 格式右键单击 ibeam 并选择保存图像以获取 png 版本,或单击此处下载 tiff 版本。

If you're making your own cursor, notice that where the black lines intersect in the original cursor is where the white lines intersect in my cursor. That's because the original cursor was meant to be used on a white background, so its black part is where the cursor hotspot is.

如果您正在制作自己的光标,请注意黑线与原始光标的相交处就是白线与我的光标相交的地方。那是因为原来的光标是要在白色背景上使用的,所以它的黑色部分是光标热点所在的地方。

回答by Eric Goldberg

I created a public repofor an ibeam cursor that should work on light and dark colored backgrounds, because it is black with a white outline. It includes succinct instructions, and a TIFF file ready to drop in to the Xcode bundle.

我为 ibeam 光标创建了一个公共存储库,它应该适用于浅色和深色背景,因为它是黑色的,带有白色轮廓。它包括简洁的说明和准备放入 Xcode 包的 TIFF 文件。

Update: the git repo includes a shell script that makes installation quite easy.

更新:git repo 包含一个 shell 脚本,使安装变得非常容易。

回答by Nicholas Riley

Unfortunately this is a difference between Carbon and Cocoa cursors—Cocoa cursors won't invert; at least, up to Snow Leopard. In Lion, even Carbon cursors behave like you don't want.

不幸的是,这是 Carbon 和 Cocoa 游标之间的区别——Cocoa 游标不会反转;至少,直到雪豹。在 Lion 中,即使是 Carbon 光标也表现得像您不想要的。

If you've got a machine running Snow Leopard or earlier, compare the I-beam behavior in BBEdit or TextWrangler (which use Carbon cursors), for example; it'll become entirely white on a black background. Even this is a bit fragile—when I change the screen magnification, BBEdit's formerly-white cursor becomes black.

如果您有一台运行 Snow Leopard 或更早版本的机器,请比较 BBEdit 或 TextWrangler(使用 Carbon 光标)中的工字梁行为,例如;它会在黑色背景上完全变成白色。即使这有点脆弱——当我改变屏幕放大倍数时,BBEdit 以前的白色光标变成黑色。

You can still set a Carbon cursor in your Cocoa app. Try this in a NSTextView subclass:

您仍然可以在 Cocoa 应用程序中设置碳光标。在 NSTextView 子类中试试这个:

#import <Carbon/Carbon.h>
[...]

- (void)resetCursorRects;
{
    // disable existing cursor setting behavior
}

- (void)cursorUpdate:(NSEvent *)event;
{
    SetThemeCursor(kThemeIBeamCursor);
}

- (void)updateTrackingAreas;
{
    for (NSTrackingArea *trackingArea in [self trackingAreas])
        [self removeTrackingArea:trackingArea];

    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:NSTrackingCursorUpdate | NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

(In 64-bit you'll see that this function is excluded from the headers, but the symbol is there and works.) If you're sufficiently motivated, you could potentially patch Xcode to do some variant of the above, perhaps from an Xcode plugin.

(在 64 位中,你会看到这个函数被排除在头文件之外,但符号在那里并且有效。)如果你有足够的动力,你可以修补 Xcode 来做上述的一些变体,也许来自Xcode 插件

If you're on Lion, here's what it is supposed to look like:

如果您使用的是 Lion,它应该是这样的:

Inverting cursor

反转光标

So I'd suggest filing a bug with Apple to make the systemwide I-beam cursor (or NSCursor in general) properly handle dark backgrounds; it's not an Xcode-specific issue and it should really have been fixed years ago.

因此,我建议向 Apple 提交一个错误,以使系统范围的 I 型光标(或一般的 NSCursor)正确处理深色背景;这不是 Xcode 特定的问题,它应该在几年前就已经解决了。

回答by Ellis

For Xcode 5 preview, i have created a better I-Beam for Dark color templates. You can use it freely. http://www.scigems.org/wordpress/?p=92

对于 Xcode 5 预览,我为深色模板创建了更好的 I-Beam。您可以自由使用它。 http://www.scigems.org/wordpress/?p=92

回答by Julian Richardson

According to this thread on the Apple forums:

根据苹果论坛上的这个帖子:

https://discussions.apple.com/thread/2811447?start=0&tstart=0

https://discussions.apple.com/thread/2811447?start=0&tstart=0

the "cursor color" referred to in the XCode 4 fonts and colors preferences is actually the cursor you see when you have clicked in a window to enter text.

XCode 4 字体和颜色首选项中提到的“光标颜色”实际上是您在窗口中单击以输入文本时看到的光标。

The I-beam cursor you see when you are moving the mouse around in the code editing window is a Mac-OS wide feature which you cannot change in XCode.

当您在代码编辑窗口中移动鼠标时看到的工字梁光标是 Mac-OS 范围内的功能,您无法在 XCode 中更改。

The forum post above suggests looking for third party software to change attributes of the system-wide I-beam cursor.

上面的论坛帖子建议寻找第三方软件来更改系统范围内的工字形光标的属性。

Another post points out that you can at least edit the cursor size to make it more visible:

另一篇文章指出,您至少可以编辑光标大小以使其更明显:

System Preferences -> Universal Access -> Mouse & Trackpad -> Cursor Size

系统偏好设置 -> 通用访问 -> 鼠标和触控板 -> 光标大小