
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];
}
}
总结以上是内存溢出为你收集整理的Cocoa中的Text全部内容,希望文章能够帮你解决Cocoa中的Text所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)