如何在iPhone中合并两个UITextView?

时间:2012-02-03 07:17:57

标签: iphone objective-c scroll uitextview

我在textview中使用自动填充文字,因此我使用两个UITextView第一个UITextViewduplicateTextView)是背景文字,如水印文字,第二个UITextViewcustomTextView)是用户输入文本。当用户在textview中输入多行文本时,customTextView会自动滚动,但duplicateTextView不会滚动。那么,如何在输入多行文字时滚动duplicateTextView?可以在iPhone中合并两个UITextView吗?

这里我尝试了源代码:

duplicateTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, 320, 100)];
duplicateTextView.delegate = self;
duplicateTextView.scrollEnabled = YES;
duplicateTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
duplicateTextView.textColor = [UIColor lightGrayColor];
[self.view addSubview:duplicateTextView];

customTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
customTextView.layer.borderWidth = 2.0;
customTextView.delegate = self;
customTextView.backgroundColor = [UIColor clearColor];
customTextView.layer.borderColor = [UIColor blackColor].CGColor;
customTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
customTextView.textColor = [UIColor blackColor];
[duplicateTextView addSubview:customTextView];

1 个答案:

答案 0 :(得分:0)

正如@KAREEM MAHAMMED试图解释的那样,你在你发布的最后一行代码中将customTextView添加到你的duplicateTextView中。也许你打算做以下事情: [self.view addSubview:customTextView];

也许我误解了你的问题。我在这里添加了一些代码,所以你可以看到一个有效的例子:

.h文件:

#import <UIKit/UIKit.h>

@class TestViewController;

@interface TestAppDelegate : 
NSObject <UIApplicationDelegate, UIScrollViewDelegate, UITextViewDelegate> 
{
    UIWindow *window;
    TestViewController *viewController;

    UITextView *customTextView;
    UITextView *duplicateTextView;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet TestViewController *viewController;

-(void)syncScroll;

@end

和.m文件:

#import "TestAppDelegate.h"
#import "TestViewController.h"

@implementation TestAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // custom
    customTextView = [[[UITextView alloc] initWithFrame:CGRectMake(10.0, 10.0, 80.0, 125.0)] autorelease];
    customTextView.backgroundColor = [UIColor whiteColor];
    [viewController.view addSubview:customTextView];

    // duplicate
    duplicateTextView = [[[UITextView alloc] initWithFrame:CGRectMake(170.0, 10.0, 80.0, 125.0)] autorelease];
    duplicateTextView.backgroundColor = [UIColor whiteColor];
    duplicateTextView.editable = NO;
    duplicateTextView.scrollEnabled = NO;
    [viewController.view addSubview:duplicateTextView];

    // whenever text is entered into the customTextView then sync scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(syncScroll)
                                                 name:UITextViewTextDidChangeNotification 
                                               object:customTextView];
    customTextView.delegate = self;

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    // have keyboard show up in customTextView
    [customTextView becomeFirstResponder];

    return YES;
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

#pragma mark -
#pragma mark Other Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self syncScroll];
}

-(void)syncScroll {
    duplicateTextView.text = customTextView.text;
    [duplicateTextView setContentOffset:customTextView.contentOffset];  
}


@end