文本的显示与编辑

来源:互联网 发布:淘宝上算命高人推荐 编辑:程序博客网 时间:2024/05/23 01:55

我们以前用UILabel来显示文本,但是在UILabel中当文本长度超过显示区域时并不能显示滚动条,但是我们用UITextView可以很轻松的做到这一点,下面给大家分享一下

代码如下:

HHLAppDelegate.h

#import <UIKit/UIKit.h>@class HHLViewController;@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HHLViewController *viewController;@end

HHLAppDelegate.m

#import "HHLAppDelegate.h"#import "HHLViewController.h"@implementation HHLAppDelegate- (void)dealloc{    [_window release];    [_viewController release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];    self.window.rootViewController = self.viewController;    [self.window makeKeyAndVisible];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

HHLViewController.h

#import <UIKit/UIKit.h>@interface HHLViewController : UIViewController@end


HHLViewController.m

#import "HHLViewController.h"@interface HHLViewController ()@end@implementation HHLViewController- (void)viewDidLoad{    [super viewDidLoad];UITextView *textView = [[[UITextView alloc] init]autorelease];    textView.frame = self.view.bounds;    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;    textView.editable = NO;  //不可编辑        textView.backgroundColor = [UIColor blackColor];    textView.textColor = [UIColor whiteColor];    textView.font = [UIFont systemFontOfSize:32];    textView.text = @"Hello,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)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


运行后效果如下图:


如果想编辑文本时,非常的简单只需要改变一句代码就可以了,textView.editable = YES;当然了也可以把textView.editable = NO;给注释掉,因为它的缺省值为YES;

注释后触摸屏幕效果如下图所示:


我们发现有一部分文字被挡到了,怎么解决呢,我们可以在编辑状态时,把textView的高度减小到键盘的上边就好了。在非编辑状态下,TextView的高度和屏幕的高度一致。

这个小技巧非常的有用哦,因为在 短信,微信,QQ,等各种聊天,即时通讯软件中,在对话框的界面都需要实现这样的功能,大家一定要记下啊

具体的代码实现如下:


HHLAppDelegate.h

#import <UIKit/UIKit.h>@class HHLViewController;@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HHLViewController *viewController;@property (strong,nonatomic)UINavigationController *myNavigationController;@end


HHLAppDelegate.m

#import "HHLAppDelegate.h"#import "HHLViewController.h"@implementation HHLAppDelegate- (void)dealloc{    [_window release];    [_viewController release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];            UINavigationController *pNaVC = [[UINavigationController alloc]initWithRootViewController:self.viewController];    self.myNavigationController = pNaVC;    [pNaVC release];    self.window.rootViewController = self.myNavigationController;    [self.window makeKeyAndVisible];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

HHLViewController.h


#import <UIKit/UIKit.h>@interface HHLViewController : UIViewController<UITextViewDelegate>{@private    UITextView *textView;    }@end

HHLViewController.m

#import "HHLViewController.h"@interface HHLViewController ()@end@implementation HHLViewController- (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];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


运行后效果如下所示:

是不是不被遮挡了,当然了这个程序还有一个问题没有解决就是,还没有实现中文输入法,这个在以后的博客里会提到,希望大家多多关关注我哦

0 0
原创粉丝点击