我们以前用UILabel来显示文本,但是在UILabel中当文本长度超过显示区域时并不能显示滚动条,但是我们用UITextView可以很轻松的做到这一点,下面给大家分享一下
代码如下:
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]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