现在的位置: 首页 > 综合 > 正文

UITextView 加载其他View

2013年10月15日 ⁄ 综合 ⁄ 共 2906字 ⁄ 字号 评论关闭

#import <UIKit/UIKit.h>

@interface UITestViewController : UIViewController <UITextViewDelegate>

{

}

@end


#import "UITestViewController.h"

UIScrollView *myScrollView;

UITextView *myTextView;


@implementation UITestViewController


-(void)buttonClick:(id)sender

{

[myTextView resignFirstResponder];

}

- (void)viewDidLoad {

        [super viewDidLoad];

       CGRect viewRect = [self.view bounds];

        myScrollView = [[UIScrollView alloc] initWithFrame:viewRect];

        myScrollView.contentSize = viewRect.size;

        [self.view addSubview:myScrollView];


        
CGRect buttonFrame = CGRectMake(10,10,60,32);

        UIButton *keyboardToggle = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        [keyboardToggle setTitle:@"hide" forState:UIControlStateNormal];

        [keyboardToggle addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        keyboardToggle.frame = buttonFrame;

        [myScrollView addSubview:keyboardToggle];


        CGRect textRect = CGRectMake(10,60,300,200);
        myTextView = [[UITextView alloc] initWithFrame:textRect];

        myTextView.font = [UIFont systemFontOfSize:22.0];

        myTextView.keyboardType = UIKeyboardTypeDefault;
        myTextView.returnKeyType = UIReturnKeyGo;

        /uncomment to enable data detectors
        //myTextView.text = @"this is a link: http://google.com ";
        //myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
        //myTextView.enabled = NO;

        myTextView.delegate = self;

         [myScrollView addSubview:myTextView];

         [myTextView release]; 
         [myScrollView release];
 }

 - (void)textViewDidBeginEditing:(UITextView *)textView 
 { 
         float keyboardHeight;
         if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait | UIDeviceOrientationPortraitUpsideDown)
         keyboardHeight = 216.0;
         else
         keyboardHeight = 162.0;

         CGRect textViewRect = textView.frame;
         float textViewBottom = textViewRect.origin.y + textViewRect.size.height;

         CGRect viewRect = [myScrollView bounds];
         float keyboardTop = viewRect.size.height-keyboardHeight;

         float scrollOffset = fabs(textViewBottom - keyboardTop);

         [myScrollView setContentOffset:CGPointMake(0, scrollOffset) animated:YES]; 
 }

 - (void)textViewDidEndEditing:(UITextView *)textView 
 {
         [myScrollView setContentOffset:CGPointMake(0, 0) animated:YES]; 
 }
 - (void)didReceiveMemoryWarning {
         // Releases the view if it doesn't have a superview.
         [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
 }

 - (void)viewDidUnload {
         // Release any retained subviews of the main view.
         // e.g. self.myOutlet = nil;
 }

 - (void)dealloc {
         [super dealloc];
 }

 @end

一种简单的按回车键后,键盘弹起的操作,实现UITextViewDelegate的下属方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range

        replacementText:(NSString *)text {

        if ([text isEqualToString:@"\n"]) {

        [textView resignFirstResponder];

        return NO;

        }

        return YES;

【上篇】
【下篇】

抱歉!评论已关闭.