加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:46:54 所属栏目:百科 来源:网络整理
导读:好的,这就是我想要的: 我们有一个NSTextView 在光标位置获
好的,这就是我想要的:

>我们有一个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;
}}];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读