基本上,我正在尝试通过自定义WindowController类向窗口中的NSTextFIEld添加超链接.我能够让超链接处理一些问题:
>当我将鼠标悬停在超链接上时,我会得到一个’I bean'(指示您可以选择文本的光标).我想要一只通常出现在超链接上的手
>当我点击超链接文本时,它会成功打开浏览器中的链接,但随后会更改文本大小和格式(例如,它不再居中,返回默认值).现在,当我将鼠标悬停在它上面时,我得到了一只手.
经过一些实验,我发现最初的字符串格式(例如大小,我点击它之前的字体)是我创建标签的.xib文件的格式.点击之后,它变成了一些默认字体,我似乎无法以任何方式影响.但是超链接仍然存在.
这是一些代码:
AboutwindowController.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
AboutwindowController.h
#import <Cocoa/Cocoa.h>@interface AboutwindowController : NSWindowController@property (weak) IBOutlet NSTextFIEld *testLabel;@end
.xib非常简单:它是一个带有标签的窗口,我将标签正确链接到.h文件(我相信)
谢谢您的帮助.我会尝试定期回来回答任何问题/澄清.编辑:请检查我对bikram答案的评论,以了解我的情况.
解决方法 您遇到的问题是NSMutableAttributedString正在尝试强制其格式化,而NSTextFIEld正在强制它自己的格式.我的主要XIB只有菜单,我的windowController XIB有一个Label NSTextFIEld.
windowController:
@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
AppDelegate中:
@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];}总结
以上是内存溢出为你收集整理的objective-c – TextField中的NSAttributedString在单击时更改/重置全部内容,希望文章能够帮你解决objective-c – TextField中的NSAttributedString在单击时更改/重置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)