2012-02-03 54 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