Cocoa NSTextView实时文本高亮显示
我想创建一个文本视图,它支持突出显示基本内容,比如链接和标签。类似的功能可以在Twitter.app中找到: 不需要支持点击这些链接,只需要在用户编辑文本视图内容时正确突出显示所有内容 问题是,最好的方法是什么?我真的不想使用重量级语法突出显示库,但我没有找到任何简单的小库来突出显示一些东西Cocoa NSTextView实时文本高亮显示,cocoa,highlighting,nstextview,Cocoa,Highlighting,Nstextview,我想创建一个文本视图,它支持突出显示基本内容,比如链接和标签。类似的功能可以在Twitter.app中找到: 不需要支持点击这些链接,只需要在用户编辑文本视图内容时正确突出显示所有内容 问题是,最好的方法是什么?我真的不想使用重量级语法突出显示库,但我没有找到任何简单的小库来突出显示一些东西 我应该自己解析文本并突出显示它吗?如果应该,我可以使用哪些库来标记文本,以及哪些库允许我进行实时高亮显示?是的,如果您想要这种轻量级,请使用您自己的解析来查找相关部分,然后使用NSTextView的文本存
我应该自己解析文本并突出显示它吗?如果应该,我可以使用哪些库来标记文本,以及哪些库允许我进行实时高亮显示?是的,如果您想要这种轻量级,请使用您自己的解析来查找相关部分,然后使用NSTextView的
文本存储
来更改所查找范围的文本属性。是的,如果您希望使用轻量级,请使用自己的解析来查找相关部分,然后使用NSTextView的textStorage
来更改所查找范围的文本属性。是,如果您希望使用轻量级,请使用自己的解析来查找相关部分,然后使用NSTextView的textStorage
来更改所查找范围的文本属性。是,如果您希望使用轻量级,请使用您自己的解析来查找相关部分,然后使用NSTextView的textStorage
来更改所查找范围的文本属性。以下是一个小示例:
您需要实现此委托方法,textview1是TextView的出口:
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
NSLog(@"string is this");
NSString *allTheText =[textView1 string];
NSArray *lines = [allTheText componentsSeparatedByString:@""];
NSString *str=[[NSString alloc]init];
NSMutableAttributedString *attr;
BOOL isNext=YES;
[textView1 setString:@""];
for (str in lines)
{
attr=[[NSMutableAttributedString alloc]initWithString:str];
if ([str length] > 0)
{
NSRange range=NSMakeRange(0, [str length]);
[attr addAttribute:NSLinkAttributeName value:[NSColor greenColor] range:range];
[textView1 .textStorage appendAttributedString:attr];
isNext=YES;
}
else
{
NSString *str=@"";
NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
[textView1 .textStorage appendAttributedString:attr];
isNext=NO;
}
}
}
这将为您提供带有hyperlink;的蓝色文本 下面是一个小例子:
您需要实现此委托方法,textview1是TextView的出口:
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
NSLog(@"string is this");
NSString *allTheText =[textView1 string];
NSArray *lines = [allTheText componentsSeparatedByString:@""];
NSString *str=[[NSString alloc]init];
NSMutableAttributedString *attr;
BOOL isNext=YES;
[textView1 setString:@""];
for (str in lines)
{
attr=[[NSMutableAttributedString alloc]initWithString:str];
if ([str length] > 0)
{
NSRange range=NSMakeRange(0, [str length]);
[attr addAttribute:NSLinkAttributeName value:[NSColor greenColor] range:range];
[textView1 .textStorage appendAttributedString:attr];
isNext=YES;
}
else
{
NSString *str=@"";
NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
[textView1 .textStorage appendAttributedString:attr];
isNext=NO;
}
}
}
这将为您提供带有hyperlink;的蓝色文本 下面是一个小例子:
您需要实现此委托方法,textview1是TextView的出口:
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
NSLog(@"string is this");
NSString *allTheText =[textView1 string];
NSArray *lines = [allTheText componentsSeparatedByString:@""];
NSString *str=[[NSString alloc]init];
NSMutableAttributedString *attr;
BOOL isNext=YES;
[textView1 setString:@""];
for (str in lines)
{
attr=[[NSMutableAttributedString alloc]initWithString:str];
if ([str length] > 0)
{
NSRange range=NSMakeRange(0, [str length]);
[attr addAttribute:NSLinkAttributeName value:[NSColor greenColor] range:range];
[textView1 .textStorage appendAttributedString:attr];
isNext=YES;
}
else
{
NSString *str=@"";
NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
[textView1 .textStorage appendAttributedString:attr];
isNext=NO;
}
}
}
这将为您提供带有hyperlink;的蓝色文本 下面是一个小例子:
您需要实现此委托方法,textview1是TextView的出口:
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
NSLog(@"string is this");
NSString *allTheText =[textView1 string];
NSArray *lines = [allTheText componentsSeparatedByString:@""];
NSString *str=[[NSString alloc]init];
NSMutableAttributedString *attr;
BOOL isNext=YES;
[textView1 setString:@""];
for (str in lines)
{
attr=[[NSMutableAttributedString alloc]initWithString:str];
if ([str length] > 0)
{
NSRange range=NSMakeRange(0, [str length]);
[attr addAttribute:NSLinkAttributeName value:[NSColor greenColor] range:range];
[textView1 .textStorage appendAttributedString:attr];
isNext=YES;
}
else
{
NSString *str=@"";
NSAttributedString *attr=[[NSAttributedString alloc]initWithString:str];
[textView1 .textStorage appendAttributedString:attr];
isNext=NO;
}
}
}
这将为您提供带有hyperlink;的蓝色文本 您是否尝试过使用正则表达式来匹配文本(在后台,文本更新时)?找到匹配项后,设置(NSAttributedString的)所需属性非常简单
您可以看一看您是否尝试过使用正则表达式来匹配文本(在后台,文本更新时)?找到匹配项后,设置(NSAttributedString的)所需属性非常简单
您可以看一看您是否尝试过使用正则表达式来匹配文本(在后台,文本更新时)?找到匹配项后,设置(NSAttributedString的)所需属性非常简单
您可以看一看您是否尝试过使用正则表达式来匹配文本(在后台,文本更新时)?找到匹配项后,设置(NSAttributedString的)所需属性非常简单 你可以看看