文本视图(UITextView)

来源:互联网 发布:js floatparse 编辑:程序博客网 时间:2024/05/17 02:05

    



开篇小技巧

视图类

- (void)viewDidLoad {

  [superviewDidLoad];


  self.title =@"主屏幕";

  if ( !items_ ) {


//将类的名字放进数组

    items_ = [[NSArrayalloc] initWithObjects:

                                @"UIKitPrjTextView",

                                @"UIKitPrjEditableTextView",

                                @"UIKitPrjWorkingWithTheSelection",

                                nil ];

  } 

}


- (void)viewWillAppear:(BOOL)animated {

  [superviewWillAppear:animated];


  [self.navigationControllersetNavigationBarHidden:NOanimated:NO];

  [self.navigationControllersetToolbarHidden:NOanimated:NO];


  // 还原颜色

  [UIApplicationsharedApplication].statusBarStyle =UIStatusBarStyleDefault;

  self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

  self.navigationController.navigationBar.translucent = NO;

  self.navigationController.toolbar.barStyle = UIBarStyleDefault;

  self.navigationController.toolbar.translucent = NO;


  [UIViewsetAnimationsEnabled:YES];

}


#pragma mark UITableView methods


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

  return [items_count];

}



- (UITableViewCell*)tableView:(UITableView*)tableView  cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

  staticNSString *CellIdentifier = @"Cell";

  

  UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell ==nil) {

    cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];

  }

  

  NSString* title = [items_objectAtIndex:indexPath.row];

  cell.textLabel.text = [titlestringByReplacingOccurrencesOfString:@"UIKitPrj"withString:@""];


  return cell;

}


- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

//从数组中将类的名取出, 用NSClassFromString( className )真正转化成实体类

  NSString* className = [items_objectAtIndex:indexPath.row];

  Class class =NSClassFromString( className );

  UIViewController* viewController = [[[classalloc] init]autorelease];

  if ( !viewController ) {

    NSLog(@"%@ 没有数据.", className );

    return;

  } 

  [self.navigationControllerpushViewController:viewControlleranimated:YES];

}




UITextView* textView = [[[UITextViewalloc] init]autorelease];

  textView.frame =self.view.bounds;

  textView.autoresizingMask =

    UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;

  //textView.editable = NO; //<不可编辑


  textView.backgroundColor = [UIColorblackColor]; //<背景为黑色

  textView.textColor = [UIColorwhiteColor]; //<字符为白色

  textView.font = [UIFontsystemFontOfSize:32];//< 字体的设置

  textView.text =@"学习UITextView!\n"

                   "2\n"

                   "3\n"

                   "4\n"

                   "5\n"

                   "6\n"

                   "7\n"

                   "8\n"

                   "9\n"

                   "10\n"

                   "11\n"

                   "12\n";


  [self.viewaddSubview:textView];




- (void)dealloc {

  [textView_release];  

  [superdealloc];

}


- (void)viewDidLoad {

  [superviewDidLoad];


  textView_ = [[UITextViewalloc] init];

  textView_.frame =self.view.bounds;

  textView_.autoresizingMask =UIViewAutoresizingFlexibleWidth |

                               UIViewAutoresizingFlexibleHeight;

  textView_.delegate =self;

  textView_.text =@"亲们,可以编辑这一段文本。";


  [self.viewaddSubview:textView_];

}


- (void)viewWillAppear:(BOOL)animated {

  [superviewWillAppear:animated];

  [self.navigationControllersetNavigationBarHidden:NOanimated:YES];

  [self.navigationControllersetToolbarHidden:NOanimated:YES];

}


- (void)viewDidAppear:(BOOL)animated {

  [superviewDidAppear:animated];

  [selftextViewDidEndEditing:textView_];//< 画面显示时设置为非编辑模式

}


- (void)viewWillDisappear:(BOOL)animated {

  [superviewWillDisappear:animated];

  [textView_resignFirstResponder]; //<画面跳转时设置为非编辑模式

}


- (void)textViewDidBeginEditing:(UITextView*)textView {

  staticconst CGFloat kKeyboardHeight =216.0;


  // 按钮设置为[完成]

  self.navigationItem.rightBarButtonItem =

    [[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone

                                                  target:self

                                                  action:@selector(doneDidPush)]autorelease];

  [UIViewbeginAnimations:nilcontext:nil];

  [UIViewsetAnimationDuration:0.3];

  // 缩小UITextView以免被键盘挡住

  CGRect textViewFrame = textView.frame;

  textViewFrame.size.height =self.view.bounds.size.height - kKeyboardHeight;

  textView.frame = textViewFrame;

  // 工具条位置上移

  CGRect toolbarFrame =self.navigationController.toolbar.frame;

  toolbarFrame.origin.y =

    self.view.window.bounds.size.height - toolbarFrame.size.height - kKeyboardHeight;

  self.navigationController.toolbar.frame = toolbarFrame;

  [UIViewcommitAnimations];

}


- (void)textViewDidEndEditing:(UITextView*)textView {

  // 按钮设置为[编辑]

  self.navigationItem.rightBarButtonItem =

    [[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit

                                                  target:self

                                                  action:@selector(editDidPush)]autorelease];

  [UIViewbeginAnimations:nilcontext:nil];

  [UIViewsetAnimationDuration:0.3];

  // 恢复UITextView的尺寸

  textView.frame =self.view.bounds;

  // 恢复工具条的位置

  CGRect toolbarFrame =self.navigationController.toolbar.frame;

  toolbarFrame.origin.y =

    self.view.window.bounds.size.height - toolbarFrame.size.height;

  self.navigationController.toolbar.frame = toolbarFrame;

  [UIViewcommitAnimations];

}


- (void)editDidPush {

  [textView_becomeFirstResponder];

}


- (void)doneDidPush {

  [textView_resignFirstResponder];

}






#import "UIKitPrjWorkingWithTheSelection.h"


static constCGFloat kKeyboardHeight = 216.0;


@implementation UIKitPrjWorkingWithTheSelection


- (void)dealloc {

  [textView_release];  

  [superdealloc];

}


- (void)viewDidLoad {

  [superviewDidLoad];


  // UITextView的追加

  textView_ = [[UITextViewalloc] init];

  textView_.frame =self.view.bounds;

  textView_.autoresizingMask =UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;

  textView_.text =@"此文本可编辑。";

  [self.viewaddSubview:textView_];


  // 在工具条中追加按钮

  UIBarButtonItem* hasTextButton =

    [[[UIBarButtonItemalloc] initWithTitle:@"hasText"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(hasTextDidPush)]autorelease];

  UIBarButtonItem* selectionButton =

    [[[UIBarButtonItemalloc] initWithTitle:@"selection"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(selectionDidPush)]autorelease];

  UIBarButtonItem* alignmentButton =

    [[[UIBarButtonItemalloc] initWithTitle:@"alignment"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(alignmentDidPush)]autorelease];

  UIBarButtonItem* scrollButton =

    [[[UIBarButtonItemalloc] initWithTitle:@"top"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(scrollDidPush)]autorelease];

  NSArray* buttons = [NSArrayarrayWithObjects:hasTextButton, selectionButton, alignmentButton, scrollButton,nil];

  [selfsetToolbarItems:buttons animated:YES];

}


- (void)viewDidAppear:(BOOL)animated {

  [superviewDidAppear:animated];


  // 调整工具条位置

  [UIViewbeginAnimations:nilcontext:nil];

  [UIViewsetAnimationDuration:0.3];

  textView_.frame =

    CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height - kKeyboardHeight );

  CGRect toolbarFrame =self.navigationController.toolbar.frame;

  toolbarFrame.origin.y =

    self.view.window.bounds.size.height - toolbarFrame.size.height -kKeyboardHeight;

  self.navigationController.toolbar.frame = toolbarFrame;

  [UIViewcommitAnimations];

  [textView_becomeFirstResponder]; //<画面显示时显示键盘

}


- (void)viewWillDisappear:(BOOL)animated {

  [superviewWillDisappear:animated];


  // 恢复工具条

  [UIViewbeginAnimations:nilcontext:nil];

  [UIViewsetAnimationDuration:0.3];

  textView_.frame =self.view.bounds;

  CGRect toolbarFrame =self.navigationController.toolbar.frame;

  toolbarFrame.origin.y =self.view.window.bounds.size.height - toolbarFrame.size.height;

  self.navigationController.toolbar.frame = toolbarFrame;

  [UIViewcommitAnimations];

  [textView_resignFirstResponder]; //<画面隐藏时隐藏键盘

}


- (void)hasTextDidPush {

  UIAlertView* alert = [[[UIAlertViewalloc] init]autorelease];

  if (textView_.hasText ) {

    alert.message =@"textView_.hasText = YES";

  } else {

    alert.message =@"textView_.hasText = NO";

  }

  [alert addButtonWithTitle:@"OK"];

  [alert show];

}


- (void)selectionDidPush {

  UIAlertView* alert = [[[UIAlertViewalloc] init]autorelease];

  alert.message = [NSStringstringWithFormat:@"location = %d, length = %d",

                    textView_.selectedRange.location,textView_.selectedRange.length];

  [alert addButtonWithTitle:@"OK"];

  [alert show];

}


- (void)alignmentDidPush {

  textView_.editable =NO;

  if (UITextAlignmentRight < ++textView_.textAlignment ) {

    textView_.textAlignment =UITextAlignmentLeft;

  } 

  textView_.editable =YES;

}


- (void)scrollDidPush {

  // NSRange scrollRange = NSMakeRange( 0, 1 );

  [textView_scrollRangeToVisible:NSMakeRange(0, 1 )];

}



0 0
原创粉丝点击