Cocoa中的Text

来源:互联网 发布:酷狗音乐无法连接网络 编辑:程序博客网 时间:2024/05/01 12:50

Interface Builder提供了一些控件来使用TEXT,但它们一般都继承自:

NSTextField:显示表态或动态text

NSTextView:可以使用Text中的多行

Text基本用法:此处实现在广本框内对文本及背景颜色进行设置,以及相关格式进行修改

创建用户界面

在Xcode中新建一个项目,

打开XIB项目中的Window窗口,

拉入窗口中一个Text View(NStextView),两个Check Box(NSButton)(Apply to selection only(选中) , ruler(不选)),两个Color Well(NSColorWell)和两个Lable(NSTextField)(Text , Background)

加入中间控制类

拉入XIB项目中一个Object

打开新建对象的Object identity窗口

Class处类名设置为:MyController

添加四个OutLets:[applyCheckbox],[backgroundColorWell],[textColorWell],[textView]

添加三个actions:[setBackgroundColor:],[setTextColor:],[toggleRuler:]

绑定界面控件

outlets:

applyCheckbox<>Apply to selection only , textView<>ruler

backgroundColorWell 绑定到由Label(Background)标记的color well

textColorWell 绑定到由Lable( text)标记的 color well

actions:

toggleRuler<>ruler

setTextColor<>由Lable( text)标记的 color well

setBackgroundColor<>由Label(Background)标记的color well

选中XIB项目中MyController,File  ->  Write Class

实现代码方法

interface 文件(*.h)

#import <Cocoa/Cocoa.h>

@interface MyController : NSObject {

    IBOutlet id applyCheckBox;

    IBOutlet id backgroundColorWell;

    IBOutlet id textColorWell;

    IBOutlet id textView;

}

- (IBAction)setBackgroundColor:(id)sender;

- (IBAction)setTextColor:(id)sender;

- (IBAction)toggleRuler:(id)sender;

@end

 

implementation 文件(*.m)

#import "MyController.h"

@implementation MyController

- (IBAction)setBackgroundColor:(id)sender {

 [textView setBackgroundColor:

[backgroundColorWell color]];

}

- (IBAction)setTextColor:(id)sender {

if([applyCheckBox state]){

[textView setTextColor:

[textColorWell color]

range:[textView selectedRange]];

}

else {

[textView setTextColor:[textColorWell color]];

}

}

-(void)awakeFromNib{

[textColorWell setColor:[NSColor blackColor]];

[backgroundColorWell setColor:[NSColor whiteColor]];

}

- (IBAction)toggleRuler:(id)sender {

[textView toggleRuler:[sender state]];

}

@end

 

可以在打开一个窗口后通过 command + T 或者 Format  ->  Font  ->  Show Fonts,打开一个窗口,在这个窗口中进行格式的设置

可以使用系统带有的粘贴板,也可以通过快截键操作

通过程序编辑TextView

在其中显示信息:[textView setString:someStringToShow];

替换部分内容

NSRange theRange;

theRange=NSMakeRange(2,4);//选中从第2个字符开始的4个字符

[textView replaceCharactersInRange:theRange withString:@"hate"];

选中所有内容

theRange=NSMakeRange(0,[[textView string] length]);

[textView setSelectedRange:theRange];

保存Text

有简单(没有任何格式)和丰富(保存所有格式)两种保存方式

简单方式

-(IBAction)savePlainTextFile:(id)sender{

NSSavePanel *savePanel=[NSSavePanel savePanel];

[savePanel setRequiredFileType:@"txt"];

[savePanel setTitle:@"SaveAsPlainText"];

if([savePanel runModal]==NSOKButton){

[[textView string] writeToFile:[savePanel fileName]

atomically:YES encoding:NSUTF8StringEncoding error:NULL];

丰富方式

-(IBAction)saveRichTextFile:(id)sender{

NSSavePanel *savePanel=[NSSavePanel savePanel];

[savePanel setRequiredFileType:@"rtf"];

[savePanel setTitle:@"SaveAsRichText"];

if([savePanel runModal]==NSOKButton){

[[textView RTFFromRange:NSMakeRange(0,[[textView string] length])]

writeToFile:[savePanel fileName] atomically:YES];

}

}

读取Text

同样也有两种与保存时对应的方式查看内容

-(IBAction)openPlainTextFile:(id)sender{

NSOpenPanel *theOpenPanel=[NSOpenPanel openPanel];

if([theOpenPanel runModal]==NSOKButton){

NSString *theFileName=[theOpenPanel fileName];

NSString *theFileContents=[NSString stringWithContentsOfFile:theFileName];

[textView setString:theFileContents];

}

}

-(IBAction)openRichTextFile:(id)sender{

NSOpenPanel *theOpenPanel=[NSOpenPanel openPanel];

if([theOpenPanel runModal]==NSOKButton){

NSString *theFileName=[theOpenPanel fileName];

NSData *theRTFData=[NSData dataWithContentsOfFile:theFileName];

[textView replaceCharactersInRange:NSMakeRange(0,[[textView string] length]) withRTF:theRTFData];

}

}

 

原创粉丝点击