6

我正在使用这个类别(对吗?)http://www.nightproductions.net/references/dsclickableurltextfield_reference.html#setAttributedStringValue

实现可点击的文本字段。我已经在我的控制器类中导入了头文件,并像这样设置它的属性字符串值

NSAttributedString* str = [[NSAttributedString alloc] initWithString:@"http://www.yahoo.com"];
[url setAttributedStringValue:(NSAttributedString *)str];

[str release];

文本字段不可选择且不可编辑。

文本字段值已设置,但不可点击且不是链接。

提前致谢。

4

1 回答 1

9

我找到了另一种在 NSTextField 中显示链接的简单方法。您可以使用 HTML。这是一个简短的例子:

-(NSAttributedString *)stringFromHTML:(NSString *)html withFont:(NSFont *)font
{
  if (!font) font = [NSFont systemFontOfSize:0.0];  // Default font
  html = [NSString stringWithFormat:@"<span style=\"font-family:'%@'; font-size:%dpx;\">%@</span>", [font fontName], (int)[font pointSize], html];
  NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
  NSAttributedString* string = [[NSAttributedString alloc] initWithHTML:data documentAttributes:nil];
  return string;
}

现在您可以调用文本字段的方法:

// @property IBOutlet NSTextField *tf;
[tf setAllowsEditingTextAttributes: YES];
[tf setSelectable:YES];
NSString *credits = @"Visit our <a href=\"http://www.webpage.com\">webpage</a>";
[tf setAttributedStringValue:[self stringFromHTML:credits withFont:[tf font]]];
于 2012-11-15T05:39:07.357 回答