文本视图(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 )];
}
- UITextView/文本视图
- 文本视图(UITextView)
- 文本视图 UITextView
- iOS基本控件-UITextView 文本视图
- iOS-UITextView-文本输入视图的使用
- iOS中 UITextView文本视图 技术分享
- UITextView 文本换行
- UITextView 文本换行
- UITextView 文本换行
- ios UITextView 提示文本
- UITextView限制文本长度
- UITextView富文本
- UITextView添加默认文本
- iOS UITextView 默认文本
- 给UITextView 头部插入视图
- UITextView文本居上显示
- 使用UITextView加载html文本
- UITextView 限制输入文本长度
- mac 中Tomcat的安装 以及注意事项
- mapper.xml内容解析
- 1012--反射!!!
- 虚拟机nat模式连接
- 分享一个制作JQ插件方法的文章
- 文本视图(UITextView)
- base-adapter-helper之recyclerview版本,支持多种viewtype布局
- MessageBox
- rpm 命令
- Apache Tomcat 安装与调试
- 从TransactionTooLargeException谈到binder的内存管理
- 手把手教你写基于C++ Winsock的图片下载的网络爬虫
- Oracle Bi Publisher 报表的整合
- Unity3D 图集分割