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]];
可以在打開一個視窗後通過 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{
[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{
NSData *theRTFData=[NSData dataWithContentsOfFile:theFileName];
[textView replaceCharactersInRange:NSMakeRange(0,[[textView string] length]) withRTF:theRTFData];