Text display and editing

Source: Internet
Author: User

We used UILabel to display text, but in UILabel, when the text length exceeds the display area, the scroll bar cannot be displayed. However, we can use UITextView to easily do this, I would like to share with you the following:

The Code is as follows:

HHLAppDelegate. h

#import 
 
  @class HHLViewController;@interface HHLAppDelegate : UIResponder 
  
   @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 
 
  @interface HHLViewController : UIViewController@end
 


HHLViewController. m

# Import "HHLViewController. h "@ interface HHLViewController () @ end @ implementation HHLViewController-(void) viewDidLoad {[super viewDidLoad]; UITextView * textView = [[[UITextView alloc] init] orautelease]; textView. frame = self. view. bounds; textView. autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; textView. editable = NO; // textView cannot be edited. backgroundColor = [UIColor blac KColor]; textView. textColor = [UIColor whiteColor]; textView. font = [UIFont systemFontOfSize: 32]; textView. text = @ "Hello, UITextView! \ N "" 2 rows \ n "" 3 rows \ n "" 4 rows \ n "" 5 rows \ n "" 6 rows \ n "" 7 lines \ n "" 8 lines \ n "" 9 lines \ n "" 10 lines \ n "" 11 lines \ n "" 12 lines \ n"; [self. view addSubview: textView];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end


The effect after running is as follows:


To edit the text, you only need to change the code, textView. editable = YES; of course, you can also set textView. editable = NO; comment out, because its default value is YES;

The following figure shows how to touch the screen after commenting: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.bkjia.com/uploadfile/Collfiles/20131231/2013123109454042.png" alt = "\">

We found that some text was blocked. How can we solve this problem? When editing the status, we can reduce the height of textView to the top of the keyboard. In the non-editing status, the height of the TextView is the same as that of the screen.

This tips is very useful, because in text messages, QQ, and other chat and instant messaging software, such a function needs to be implemented on the dialog box interface. You must note it down.

The Code is as follows:


HHLAppDelegate. h

#import 
 
  @class HHLViewController;@interface HHLAppDelegate : UIResponder 
  
   @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 
 
  @interface HHLViewController : UIViewController
  
   {@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 = @ "This text can be edited"; [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]; // when the screen is displayed, it is set to non-edit mode}-(void) viewWillDisappear: (BOOL) animated {[super viewWillDisappear: animated]; [textView resignFirstResponder]; // set the mode to non-edit when the screen jumps}-(void) textViewDidBeginEditing :( UITextView *) textView {static const CGFloat kKeyboardHeight = 216.0; // The button is set to "complete" self. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action: @ selector (doneDidPush)] autorelush]; [UIView BeginAnimations: nil context: nil]; //? [UIView setAnimationDuration: 0.3]; // narrow down UITextView to avoid blocking CGRect textViewFrame = textView. frame; textViewFrame. size. height = self. view. bounds. size. height-kKeyboardHeight; textView. frame = textViewFrame; // move the toolbar up 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 {// The button is set to "edit" self. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] Destination: Custom target: self action: @ selector (editDidPush)] autorelush]; [UIView Duration: nil context: nil]; [UIView setAnimationDuration: 0.3]; // restore the size of the UITextView textView. frame = self. view. bounds; // The 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


The effect after running is as follows:

Is it not blocked? Of course, there is still a problem in this program that has not been solved, that is, the Chinese input method has not been implemented. This will be mentioned in my blog in the future. I hope you will pay more attention to me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.