2012-02-03 57 views
0

我在自动完成textview工作,所以我使用两个UITextView第一UITextViewduplicateTextView)是背景文本,如水印文本和第二UITextViewcustomTextView)是用户输入文本。当用户在textview中输入多行文本时,customTextView会自动滚动,但duplicateTextView不滚动。那么,如何在多行文字输入时滚动duplicateTextView? iPhone中可以合并两个UITextView吗?如何在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

使用aboove代码,你不能与互动duplicateTextView因为你添加了customTextView到它 – 2012-02-03 07:24:18

+0

感谢您的回复 – 2012-02-03 07:25:14

+0

这不是customTextView,只是UITextView – 2012-02-03 07:32:45

回答

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 
+0

Mr.ragragufin我已经厌倦了你的建议。但是,customTextView仍在上面滚动,而duplicateTextView仍停留在那里。谢谢。 – 2012-02-03 09:00:58

+0

首先,两个文本浏览不在同一位置。重复是在0,0和自定义在0,0。其次,当用户输入到自定义文本中时,应该将相同的文本放入重复文本中,我不认为它会自动到达那里。我认为,用户一次只能输入一个框。尝试添加代码以将用户在自定义中输入的任何内容粘贴到副本中。合理? – ragamufin 2012-02-03 09:09:40

+0

我已经添加了一些代码给我的答案澄清,请再看看。 – ragamufin 2012-02-03 10:00:26