效果如下图:
主要是实现功能,界面随便弄的。
代码如下:
ViewController。h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@property (retain, nonatomic) IBOutlet UILabel *mylabel;
@property (retain, nonatomic) IBOutlet UITextField *mytextfield;
@property (retain, nonatomic) IBOutlet UIButton *writeFileBtn;
@property (retain, nonatomic) IBOutlet UIButton *readFileBtn;
- (IBAction)writeBtnClick:(id)sender;
- (IBAction)readBtnClick:(id)sender;
@end
ViewController。m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mylabel;
@synthesize mytextfield;
@synthesize writeFileBtn;
@synthesize readFileBtn;
- (void)viewDidLoad
{
[super viewDidLoad];
// 主要实现在文本框中输入文字时,防止文本框被弹出的键盘挡住。实现UITextFieldDelegate委托中的3个方法即可解决。
mytextfield.delegate=self;
// ios6.0中UITextAlignmentCenter 已经是反对状态了,要用下面的NSTextAlignmentCenter替换
mylabel.textAlignment=NSTextAlignmentCenter;
// 这3行用来实现,label宽度的自适应, 效果相当于android布局中wrap_content
[mylabel setNumberOfLines:0];
mylabel.text=@"我是label,我用来显示文件中的内容";
[mylabel sizeToFit];
NSString *str1=@"将文本框的内容写入文件";
NSString *str2=@"文件中的内容读取label上";
[writeFileBtn setTitle:str1 forState:UIControlStateNormal];
[readFileBtn setTitle:str2 forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[mylabel release];
[mytextfield release];
[writeFileBtn release];
[readFileBtn release];
[super dealloc];
}
- (void)keyboardWillShow:(NSNotification *)noti
{
//键盘输入的界面调整
//键盘的高度
float height = 216.0;
CGRect frame = self.view.frame;
frame.size = CGSizeMake(frame.size.width, frame.size.height - height);
[UIView beginAnimations:@"Curl"context:nil];//动画开始
[UIView setAnimationDuration:0.30];
[UIView setAnimationDelegate:self];
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
if(offset > 0)
{
CGRect rect = CGRectMake(0.0f, -offset,width,height);
self.view.frame = rect;
}
[UIView commitAnimations];
}
//写文件的操作
-(void)writeFile:(NSString *)string
{
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//获取路径
//1、参数NSDocumentDirectory要获取的那种路径
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//2、得到相应的Documents的路径
NSString *str = [paths objectAtIndex:0];
//3、更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[str stringByExpandingTildeInPath]];
//4、创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
[fileManager removeItemAtPath:@"mytest" error:nil];
NSString *path = [str stringByAppendingPathComponent:@"mytest"];
//5、创建数据缓冲区
NSMutableData *writer = [[NSMutableData alloc] init];
//6、将字符串添加到缓冲中
[writer appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];
//7、将其他数据添加到缓冲中
//将缓冲的数据写入到文件中
[writer writeToFile:path atomically:YES];
[writer release];
}
//读文件
- (NSString *)readFile
{
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//获取路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//获取文件路径
NSString* path = [documentsDirectory stringByAppendingPathComponent:@"mytest"];
NSData* reader = [NSData dataWithContentsOfFile:path];
return [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding];
}
//写文件的按钮的监听
- (IBAction)writeBtnClick:(id)sender {
[self writeFile:mytextfield.text];
}
//读文件的按钮的监听
- (IBAction)readBtnClick:(id)sender {
mylabel.text=[self readFile];
}
@end