▼iPhoneプログラミングメモ▼
テキストビュー

テキストビューを利用するプログラムを作成する。


ソースコードの記述
TextViewEx.h
#import <UIKit/UIKit.h>

//TextViewExの宣言
@interface TextViewEx : UIViewController <UITextViewDelegate> {
    UITextView* _textView;
}
@end

TextViewEx.m
#import "TextViewEx.h"

#define BTN_START 0 
#define BTN_END   1 
#define BTN_SHOW  2 

//TextViewExの実装
@implementation TextViewEx

//アラートの表示
- (void)showAlert:(NSString*)title text:(NSString*)text {
    UIAlertView* alert=[[[UIAlertView alloc] 
        initWithTitle:title message:text delegate:nil 
        cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}

//テキストビューの生成
- (UITextView*)makeTextView:(CGRect)rect text:(NSString*)text {
    UITextView* textView=[[[UITextView alloc] init] autorelease];
    [textView setFrame:rect];
    [textView setText:text];     
    [textView setKeyboardAppearance:UIKeyboardAppearanceDefault];  
    [textView setKeyboardType:UIKeyboardTypeDefault];
    [textView setReturnKeyType:UIReturnKeyDefault];
    return textView;
}

//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
    UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:rect];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTag:tag];
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button; 
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];
        
    //ビューの生成
    UIView* view=[[[UIView alloc] initWithFrame:
        CGRectMake(0,0,300,100)] autorelease];
    [view setBackgroundColor:[UIColor darkGrayColor]];
    [self.view addSubview:view];

    //テキストビューの生成
    _textView=[[self makeTextView:CGRectMake(1,1,298,98)
        text:@""] retain];
    [_textView setDelegate:self];
    [self.view addSubview:_textView];        
        
    //編集開始ボタンの生成
    UIButton* btnStart=[[self makeButton:CGRectMake(0,110,90,40) 
        text:@"編集開始" tag:BTN_START] retain];
    [self.view addSubview:btnStart];

    //編集完了ボタンの生成
    UIButton* btnEnd=[[self makeButton:CGRectMake(100,110,90,40) 
        text:@"編集完了" tag:BTN_END] retain];
    [self.view addSubview:btnEnd];

    //表示ボタンの生成
    UIButton* btnShow=[[self makeButton:CGRectMake(200,110,90,40) 
        text:@"表示" tag:BTN_SHOW] retain];
    [self.view addSubview:btnShow];
}

//メモリ解放
- (void)dealloc {
    [_textView release];
    [super dealloc];
}

//テキスト変更時に呼ばれる
- (BOOL)textView:(UITextView*)textView 
    shouldChangeTextInRange:(NSRange)range 
    replacementText:(NSString*)text {
    //最大140文字制限
    if (text.length>0 &&
        textView.text.length+text.length>140) {
         return NO;
    }
    return YES;
}
 
//ボタンクリック時のイベント処理
- (IBAction)clickButton:(UIButton*)sender {
    if (sender.tag==BTN_START) {
        [_textView becomeFirstResponder];
    } else if (sender.tag==BTN_END) {
        [_textView resignFirstResponder];
    } else if (sender.tag==BTN_SHOW) {
        [self showAlert:@"" text:[NSString stringWithFormat:
            @"テキストビューの文字列\n%@,%d",
            _textView.text,_textView.text.length]];
    }
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end



−戻る−