【发布时间】:2011-05-24 15:01:59
【问题描述】:
我有一个NSTextView。
我只想在NSTextView 中的选定文本中添加一个属性(NSLinkAttributeName)...
你能帮帮我吗?
谢谢。
【问题讨论】:
标签: cocoa nstextview
我有一个NSTextView。
我只想在NSTextView 中的选定文本中添加一个属性(NSLinkAttributeName)...
你能帮帮我吗?
谢谢。
【问题讨论】:
标签: cocoa nstextview
你想获取视图的 textStorage(它基本上是一个可变的属性字符串),然后将NSLinkAttributeName 属性添加到所选范围;该属性的值是要链接到的 URL。
[[textView textStorage] addAttribute: NSLinkAttributeName value: url range:[textView selectedRange]];
【讨论】:
自从我玩 ObjC 以来已经有一段时间了,但这应该可以解决问题。它将所选文本替换为原始内容,并附加您的 attr。检查通过,但请原谅任何错别字。
NSTextView *textView = ...;
NSDictionary *attributes = ...;
//Get selected text string from TextView (see Text superclass) and append attr link
NSRange selRange = [textView selectedRange];
NSMutableString *changedStr = [[[textView string] substringWithRange:selRange] mutableCopy];
[changedStr appendString:[attributes objectForKey:NSLinkAttributeName]];
//Replace the selected text range in the TextView
[textView replaceCharactersInRange:selRange withString:[NSString stringWithString:changedStr]];
[changedStr release];
查看类定义:
【讨论】:
NSLinkAttributeName 是新领域的人),而不是删除它。我的 Objective-C 有点过时了(尤其是 2011 年),但正在更新中。