TextField 中的 NSAttributedString 在单击时更改/重置

2024-05-27

我正在跟进THIS https://developer.apple.com/library/mac/qa/qa1487/_index.html来自苹果的指南,但它并没有真正正常工作。

基本上,我试图通过自定义 WindowController 类向窗口内的 NSTextField 添加超链接。我可以让超链接解决一些问题:

  • 当我将鼠标悬停在超链接上时,我会看到一个“I bean”(指示您可以选择文本的光标)。我想要一只通常出现在超链接上方的手
  • 当我单击超链接文本时,它会成功在浏览器中打开链接,但随后它会更改文本大小和格式(例如,它不再居中,回到某些默认值)。现在,当我将鼠标悬停在它上面时,我得到了手。

经过一番实验,我发现初始字符串格式(例如,单击之前的大小、字体)是我在其中创建标签的 .xib 文件的格式。单击后,它会更改为某种默认字体我似乎无法以任何方式影响。不过,超链接仍然存在。

这是一些代码:

关于WindowController.h

#import "AboutWindowController.h"

@interface AboutWindowController ()

@end

@implementation AboutWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    [self.testLabel setAllowsEditingTextAttributes:YES];
    [self.testLabel setSelectable:YES];

    NSMutableAttributedString* string1 = [[NSMutableAttributedString alloc] init];

    NSString* inString = @"Apple Computer";
    NSURL* aURL = [NSURL URLWithString:@"www.google.com"];

    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
    NSRange range = NSMakeRange(0, [attrString length]);

    [attrString beginEditing];
    [attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];

    // make the text appear in blue
    [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

    // next make the text appear with an underline
    [attrString addAttribute:
     NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];

    [attrString endEditing];


    [string1 appendAttributedString:  attrString];


    [self.testLabel setAttributedStringValue:string1];
    [self.testLabel setFont:[NSFont fontWithName:@"Helvetica" size:20]];
}
@end

关于WindowController.h

#import <Cocoa/Cocoa.h>

@interface AboutWindowController : NSWindowController
@property (weak) IBOutlet NSTextField *testLabel;

@end

.xib 非常简单:它是一个带有标签的窗口,我将标签正确链接到 .h 文件(我相信)

谢谢您的帮助。我会尝试定期回来回答任何问题/澄清。编辑:请检查我对 bikram 答案的评论以了解我的情况的更新。


你遇到的问题是NSMutableAttributedString正在尝试强制其格式化并且NSTextField是在逼迫自己。

我的主 XIB 只有菜单,而我的 windowController XIB 有一个Label NSTextField.

窗口控制器:

@interface TFTWindowController : NSWindowController

@property (weak) IBOutlet NSTextField *testLabel;

@end

@implementation TFTWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)awakeFromNib {

}

- (void)windowDidLoad
{
    [super windowDidLoad];
    [self.testLabel setAllowsEditingTextAttributes:YES];
    [self.testLabel setSelectable:YES];

    NSMutableAttributedString* string1 = [[NSMutableAttributedString alloc] init];

    NSString* inString = @"Apple Computer";
    NSURL* aURL = [NSURL URLWithString:@"www.google.com"];

    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:inString];
    NSRange range = NSMakeRange(0, [attrString length]);

    [attrString beginEditing];
    [attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];

    // make the text appear in blue
    [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

    // next make the text appear with an underline
    [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
    [attrString addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica" size:20] range:range];

    [attrString endEditing];


    [string1 appendAttributedString:  attrString];


    [self.testLabel setAttributedStringValue:string1];
    [self.testLabel setFont:[NSFont fontWithName:@"Helvetica" size:20]];
}

@end

应用程序委托:

@interface TFTAppDelegate : NSObject <NSApplicationDelegate>

@property(nonatomic, strong)TFTWindowController *windowController;

@end

@implementation TFTAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    self.windowController = [[TFTWindowController alloc] initWithWindowNibName:@"TFTWindowController"];
    [_windowController showWindow:nil];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TextField 中的 NSAttributedString 在单击时更改/重置 的相关文章

  • QFileSystemModel setRootPath

    我正在尝试创建一个 Qt 应用程序来显示文件夹 Mac OS 中的 Users 文件夹 的内容 这是代码 QFileSystemModel dirModel new QFileSystemModel dirModel gt setRootP
  • 是否有针对不同屏幕尺寸的单独故事板?

    基本上我已经完成了一个应用程序 我唯一的问题是 ATM 机应用程序在设计时只考虑了 4 英寸显示屏 当在 3 5 英寸模拟器上运行时 应用程序会丢失 0 5 英寸 显然 那么我的问题是 如何在 Xcode 5 中为不同的屏幕尺寸设置不同的故
  • 如何使用 iPhone SDK 实现可滑动的图像堆栈(例如照片应用程序)?

    我想获取一堆图像 或者可能是一组用于下载图像的 URL 并以全屏方式显示它们 一次一个 使用 iPhone SDK 使用用户滑动来平滑地为堆栈中的下一个图像设置动画 Apple 的 Photo app 似乎可以做到这一点 此外 如果尚未检索
  • 在 Objective-C 中比较 2 个字符串

    我写了以下代码 if depSelectedIndice gt 1 comSelectedIndice gt 1 NSLog depart elemet d depSelectedIndice NSLog depart elemet d c
  • 强制 Apache HTTPD 以 32 位运行

    我通过从二进制文件 以及 ppc 部分 中剥离 32 位架构 以 64 位模式运行 Apache HTTPD 我这样做是为了使其与 python 和 mysql 更加兼容 然而 我有另一台机器需要它以 32 位模式运行 它仍然保留所有四种原
  • 使用 iPhone 中的地图视图读取当前位置名称

    我读取了当前位置的纬度和经度值 然后成功将该位置固定在 iPhone 中 现在我想使用这个纬度和经度值读取该地名 我使用以下代码来读取查找当前位置 void mapView MKMapView mapView1 didUpdateUserL
  • 在 iOS 上将 NSString 转换为 NSDate 的正确方法?

    我一直在使用此方法将常规 NSString 对象转换为 NSDate 但尝试向 Apple 提交更新 但遭到拒绝 在 iOS 中还有什么其他方法可以做到这一点 NSString date str 2011 08 12T12 20 00Z N
  • 在 XCode 中链接静态 ObjC 库的过程

    我正在尝试链接到静态库 但不断收到链接器错误 我发现了一些发布示例的网站 但我无法看到我做错了什么 首先 我创建一个链接到我的库的项目 添加 gt 现有文件找到我的 xcodeproj 文件选择 将项目复制到目标组文件夹 选择我的宿主项目作
  • 减少 CoreData 的调试输出?

    我正在开发一个使用 CoreData 的 iOS macOS 项目 它工作正常 但它会向控制台输出大量调试信息 这使得控制台无法使用 因为我的打印语句隐藏在所有与 CoreData 相关的内容中 我有一个非常简单的 CoreData 设置
  • 在 Objective C 的类方法中引用类本身

    我希望我没有跳过 ObjC 手册中的这一部分 但是是否可以从类的一个类方法中引用该类 就像在 PHP 中一样 您将使用 this 来引用当前实例 而 self 引用实例的类 this 的 ObjC 等价物将是 self 那么 PHP 的 s
  • 如何在 OSX 上安装 LaTeX .sty 文件?

    我设置了一个 LaTeX 项目 tex documents some file tex support todonotes sty where some file tex uses todonotes usepackage colorinl
  • gem install mysql:无法构建 gem 本机扩展 (Mac Lion)

    我为 Mac OS X Lion 安装了 MySQL 5 5 27 来自 dmg 现在我尝试安装 mysql gem gem install mysql Building native extensions This could take
  • 在 MAC OS X 10.9 上安装 NLTK 确实很困难

    我是 Python Mac OS 新手 我正在寻找 NLTK 教科书 但我在安装它时遇到了一些问题 我一直在寻找解决方案 但不幸的是 所有解决方案似乎都不适合我 或者我误解了如何使用它们 我遇到的基本问题是 尽管按照说明进行操作 NLTK
  • Objective-C 中是否有相当于 C++ 动态转换的功能?

    如果我有两个类 子类和超类 SuperClass super new SuperClass SubClass sub new SubClass SubClass sub pointer The nice one line cast belo
  • 未知异常和崩溃

    当我尝试快速滚动表格视图或从远程重新加载数据时 我的应用程序崩溃了 当我先进行远程获取然后滚动表格视图时 一切似乎都工作正常 我不知道下面的崩溃日志意味着什么 它只是有时工作正常 有时崩溃 Incident Identifier 710A1
  • OpenCV 无法从 MacBook Pro iSight 捕获

    几天后 我无法再从 opencv 应用程序内部打开我的 iSight 相机 cap cv2 VideoCapture 0 返回 并且cap isOpened 回报true 然而 cap grab 刚刚返回false 有任何想法吗 示例代码
  • 在 mac os Sierra 上,卡在“设置 CocoaPods 主存储库”上

    转移到 mac os sierra 后 我完全格式化了我的 mac 现在每次运行时我都面临安装 cocoapods 的问题sudo gem install cocoapods pre一切都安装得很好 当我尝试安装 Pod 时 终端堆栈打开S
  • 核心数据 iCloud 同步中的关系完整性和验证

    考虑以下简单的实体模型 实体 A 与实体 B 具有一对一关系 称为b 实体 B 具有逆对一关系 称为a 这两种关系都不是可选的 A B b lt gt a 假设我们有两个设备 1 和 2 开始完全同步 每个对象都有一个 A 类对象和一个 B
  • 在 Mac 上正确运行基于 SWT 的跨平台 jar

    我一直致力于一个基于 SWT 的项目 该项目旨在部署为 Java Web Start 从而可以在多个平台上使用 到目前为止 我已经成功解决了由于 SWT 依赖的系统特定库而出现的导出问题 请参阅相关thread https stackove
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两

随机推荐