文本视图(UITextView)

    



开篇小技巧

视图类

- (void)viewDidLoad {

  [super viewDidLoad];


  self.title = @"主屏幕";

  if ( !items_ ) {


//将类的名字放进数组

    items_ = [[NSArray alloc] initWithObjects:

                                @"UIKitPrjTextView",

                                @"UIKitPrjEditableTextView",

                                @"UIKitPrjWorkingWithTheSelection",

                                nil ];

  } 

}


- (void)viewWillAppear:(BOOL)animated {

  [super viewWillAppear:animated];


  [self.navigationController setNavigationBarHidden:NO animated:NO];

  [self.navigationController setToolbarHidden:NO animated:NO];


  // 还原颜色

  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

  self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

  self.navigationController.navigationBar.translucent = NO;

  self.navigationController.toolbar.barStyle = UIBarStyleDefault;

  self.navigationController.toolbar.translucent = NO;


  [UIView setAnimationsEnabled:YES];

}


#pragma mark UITableView methods


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

{

  return [items_ count];

}



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

{

  static NSString *CellIdentifier = @"Cell";

  

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

  }

  

  NSString* title = [items_ objectAtIndex:indexPath.row];

  cell.textLabel.text = [title stringByReplacingOccurrencesOfString:@"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 = [[[class alloc] init] autorelease];

  if ( !viewController ) {

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

    return;

  } 

  [self.navigationController pushViewController:viewController animated:YES];

}




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

  textView.frame = self.view.bounds;

  textView.autoresizingMask =

    UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

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


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

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

  textView.font = [UIFont systemFontOfSize: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.view addSubview:textView];




- (void)dealloc {

  [textView_ release];  

  [super dealloc];

}


- (void)viewDidLoad {

  [super viewDidLoad];


  textView_ = [[UITextView alloc] init];

  textView_.frame = self.view.bounds;

  textView_.autoresizingMask = UIViewAutoresizingFlexibleWidth |

                               UIViewAutoresizingFlexibleHeight;

  textView_.delegate = self;

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


  [self.view addSubview:textView_];

}


- (void)viewWillAppear:(BOOL)animated {

  [super viewWillAppear:animated];

  [self.navigationController setNavigationBarHidden:NO animated:YES];

  [self.navigationController setToolbarHidden:NO animated:YES];

}


- (void)viewDidAppear:(BOOL)animated {

  [super viewDidAppear:animated];

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

}


- (void)viewWillDisappear:(BOOL)animated {

  [super viewWillDisappear:animated];

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

}


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

  static const CGFloat kKeyboardHeight = 216.0;


  // 按钮设置为[完成]

  self.navigationItem.rightBarButtonItem =

    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone

                                                   target:self

                                                   action:@selector(doneDidPush)] autorelease];

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration: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;

  [UIView commitAnimations];

}


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

  // 按钮设置为[编辑]

  self.navigationItem.rightBarButtonItem =

    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit

                                                   target:self

                                                   action:@selector(editDidPush)] autorelease];

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration: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;

  [UIView commitAnimations];

}


- (void)editDidPush {

  [textView_ becomeFirstResponder];

}


- (void)doneDidPush {

  [textView_ resignFirstResponder];

}






#import "UIKitPrjWorkingWithTheSelection.h"


static const CGFloat kKeyboardHeight = 216.0;


@implementation UIKitPrjWorkingWithTheSelection


- (void)dealloc {

  [textView_ release];  

  [super dealloc];

}


- (void)viewDidLoad {

  [super viewDidLoad];


  // UITextView的追加

  textView_ = [[UITextView alloc] init];

  textView_.frame = self.view.bounds;

  textView_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

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

  [self.view addSubview:textView_];


  // 在工具条中追加按钮

  UIBarButtonItem* hasTextButton =

    [[[UIBarButtonItem alloc] initWithTitle:@"hasText"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(hasTextDidPush)] autorelease];

  UIBarButtonItem* selectionButton =

    [[[UIBarButtonItem alloc] initWithTitle:@"selection"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(selectionDidPush)] autorelease];

  UIBarButtonItem* alignmentButton =

    [[[UIBarButtonItem alloc] initWithTitle:@"alignment"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(alignmentDidPush)] autorelease];

  UIBarButtonItem* scrollButton =

    [[[UIBarButtonItem alloc] initWithTitle:@"top"

                                      style:UIBarButtonItemStyleBordered

                                     target:self

                                     action:@selector(scrollDidPush)] autorelease];

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

  [self setToolbarItems:buttons animated:YES];

}


- (void)viewDidAppear:(BOOL)animated {

  [super viewDidAppear:animated];


  // 调整工具条位置

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration: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;

  [UIView commitAnimations];

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

}


- (void)viewWillDisappear:(BOOL)animated {

  [super viewWillDisappear:animated];


  // 恢复工具条

  [UIView beginAnimations:nil context:nil];

  [UIView setAnimationDuration: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;

  [UIView commitAnimations];

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

}


- (void)hasTextDidPush {

  UIAlertView* alert = [[[UIAlertView alloc] 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 = [[[UIAlertView alloc] init] autorelease];

  alert.message = [NSString stringWithFormat:@"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 )];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡搜偶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值