微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 获取并突出显示NSTextView中的当前Word

好的,这就是我想要的:

>我们有一个NSTextView
>在光标位置获取“当前”字(作为NSRange?)(如何确定?)
>突出显示它(更改其属性)

我不确定如何解决这个问题:我的意思是我的主要关注点是在NSTextView中获取当前位置并获得关键点(我知道一些Text插件支持这一点,但我不确定原始的NSTextView实现. ..)

那有内置功能吗?或者,如果没有,任何想法?

更新:光标位置(已解决)

NSInteger insertionPoint = [[[myTextView selectedRanges] objectAtIndex:0] rangeValue].location;

现在,仍在尝试找到指定基础单词的解决方法……

解决方法

这是一种方式:
NSUInteger insertionPoint = [myTextView selectedRange].location;
Nsstring *string = [myTextView string];

[string enumerateSubstringsInRange:(NSRange){ 0,[string length] } options:NsstringEnumerationByWords usingBlock:^(Nsstring *word,NSRange wordRange,NSRange enclosingRange,BOOL *stop) {
if (NSLocationInRange(insertionPoint,wordRange)) {
    NSTextStorage *textStorage = [myTextView textStorage];
    NSDictionary *attributes = @{ NSForegroundColorAttributeName: [NSColor redColor] }; // e.g.
    [textStorage addAttributes:attributes range:wordRange];
    *stop = YES;
}}];

原文地址:https://www.jb51.cc/c/239707.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐