gpt4 book ai didi

objective-c - 如何设置光标处的字体?

转载 作者:行者123 更新时间:2023-12-03 17:20:47 24 4
gpt4 key购买 nike

我有一个NSTextView,我想设置光标处的字体(光标:插入点光标,闪烁条),意思是在光标处,字体变成Times New Roman(示例)。无论光标在哪里,我都希望字体改变。我尝试做

[self.textView.textStorage addAttributes:@{NSFontAttributeName: [NSFont fontWithName:@"Times New Roman" size:14]} range:NSMakeRange([[[self.textView selectedRanges] objectAtIndex:0] rangeValue].location, 0)];

但是它不起作用。还有别的办法吗?我尝试了很多方法,但仍然遇到问题。

最佳答案

首先,这将有助于避免鼠标光标(例如指向箭头或工字梁)中的“光标”与插入点或选择中的“光标”之间的混淆。

将用于新键入的文本的属性存储在 TextView 的 typingAttributes 属性中。所以,你会这样做:

NSMutableDictionary* attributes = [textView.typingAttributes mutableCopy];
attributes[NSFontAttributeName] = [NSFont fontWithName:@"Times New Roman" size:14];
self.textView.typingAttributes = attributes;

当然,如果有选择,除了设置打字属性之外,您通常还希望更改所有选定范围中的字体。您可以使用 rangesForUserCharacterAttributeChange 来实现此目的。

关于objective-c - 如何设置光标处的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28390029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com