2012年10月21日 星期日

UITextView基本使用(ㄧ)

基本的檔案管理與讀寫(一)增加UITextView的背景圖設定而來,增加了透明的背景圖

1. 增加一個背景圖


2. 在mainViewController.m加入UITextView的背景設定,並加上一個消除輸入鍵盤的機制在觸及按鍵時。
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize textEdit;
@synthesize fileName;
@synthesize filePath;

@synthesize data;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  
    fileName = @"測試檔";
    filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"txt"];
  
    textEdit.backgroundColor = [UIColor clearColor];
  
    UIImageView *a1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 500, 300)];
    [a1 setImage:[UIImage imageNamed:@"TaiwanMap.jpg"]];
    a1.alpha = 0.1f;
  
    [self.view addSubview:a1];
    [self.view sendSubviewToBack:a1];

  
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)readFile:(id)sender {
  
    if (filePath) {
      
        NSString *contentOfFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
      
        textEdit.text = contentOfFile;
    }
  
    [textEdit resignFirstResponder];
  
}
- (IBAction)clearWindow:(id)sender {
  
    textEdit.text = @"";
  
    [textEdit resignFirstResponder];
  
  
}
- (IBAction)saveFile:(id)sender {
    data = textEdit.text;
  
    if (filePath) {
      
        [data writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:nil];
      
    }
  
    [textEdit resignFirstResponder];
  
  
}

@end




4. 結果