代码之家  ›  专栏  ›  技术社区  ›  Steve McLeod

为什么超链接有时不会显示在NSTextField中的NSAttribute字符串中?

  •  5
  • Steve McLeod  · 技术社区  · 16 年前

    我在NSTextField中使用的文本是从文件加载的,如下所示。

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Credits"  ofType:@"rtf"];
    NSAttributedString *as = [[NSAttributedString alloc] initWithPath:path documentAttributes:NULL];
    [creditsLabel setAttributedStringValue:as];
    [creditsLabel becomeFirstResponder];
    

    窗口中的超链接不会以蓝色下划线呈现,除非我首先单击NSTextField上的某个位置,如两个屏幕截图所示。

    以下是RTF:

    {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250
    {\fonttbl\f0\fnil\fcharset0 LucidaGrande;}
    {\colortbl;\red255\green255\blue255;}
    \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural
    
    \f0\fs24 \cf0 Copyright \'a9 2009-2010 Fully Functional Software.\
    All rights reserved.\
    \
    BlazingStars is made possible by {\field{\*\fldinst{HYPERLINK "http://www.iterasi.net/openviewer.aspx?sqrlitid=p4mjpt7nl02tyjl08_ctaa"}}{\fldrslt DBPrefsWindowController}}, {\field{\*\fldinst{HYPERLINK "http://mattgemmell.com/source"}}{\fldrslt Image Crop}}, {\field{\*\fldinst{HYPERLINK "http://code.google.com/p/tesseract-ocr/"}}{\fldrslt Tesseract-OCR}}, {\field{\*\fldinst{HYPERLINK "http://andymatuschak.org/articles/2005/12/18/help-with-apple-help"}}{\fldrslt Andy's Help Toolkit}}, and {\field{\*\fldinst{HYPERLINK "http://wafflesoftware.net/shortcut/"}}{\fldrslt Shortcut Recorder}}.\
    \
    Includes icons from the BlueCons set by {\field{\*\fldinst{HYPERLINK "http://www.mouserunner.com"}}{\fldrslt Ken Saunders}}.}
    

    alt text
    (来源: pokercopilot.com )

    alt text
    (来源: pokercopilot.com

    2 回复  |  直到 7 年前
        1
  •  7
  •   Peter Hosey    16 年前

    QA1487 )我在你对Diederik Hoogenboom的回答的评论中看到,你已经完成了最后一部分,所以现在你需要做的就是在RTF中洒上蓝色和下划线。

    另一种解决方案是使用文本视图而不是文本字段。

    第三种解决方案是使用 DSClickableTextField .

        2
  •  2
  •   diederikh    16 年前

    [creditsLabel setAllowsEditingTextAttributes: YES] ,否则链接将无法单击。

    推荐文章