如何在iPhone中合并两个UITextView?

发布于 2025-01-02 06:32:20 字数 1187 浏览 2 评论 0原文

我在文本视图中的自动完成文本中工作,所以我使用两个 UITextView 第一个 UITextView (duplicateTextView) 是背景文本,如水印文本和第二个 UITextView (customTextView) 是用户输入文本。当用户在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];

I am worked in autocomplete text in textview, so I am using two UITextView the first UITextView (duplicateTextView) is background text like watermark text and the second UITextView (customTextView) is user entering the text. When the user enters multiline text in textview, the customTextView scrolls automatically but the duplicateTextView is not scrolling. So, how can I scroll the duplicateTextView while the multiline text is enters? Is it possible to merge two UITextView together in iPhone?

Here I tried the source code:

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖正好 2025-01-09 06:32:20

正如@KAREEM MAHAMMED 试图解释的那样,您将在发布的最后一行代码中将 customTextView 添加到重复的TextView 中。也许您打算执行以下操作:
[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

As @KAREEM MAHAMMED tried to explain, you're adding customTextView to your duplicateTextView in the last line of code you posted. Perhaps you meant to do the following instead:
[self.view addSubview:customTextView];

Perhaps I misunderstood your question. I'm adding some code here so you can see a working example:

.h file:

#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

and the .m file:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文