【问题标题】:It is possible to track the text enter into nextline in UITextView iPhone?是否可以在 UITextView iPhone 中跟踪输入下一行的文本?
【发布时间】:2012-07-20 14:55:08
【问题描述】:

我正在使用 UITextView for iPhone Messaging 应用程序。我在 UITextView 上添加了一个 UIImage 作为子视图。我想调整 UIImage 框架大小,如 UITextView 框架高度。现在我已经对此进行了编码,但是当文本进入下一行时,图像帧高度没有增加。在我们输入更多字符后,图像帧大小会增加。

    messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
    messageTextView.delegate = self;
    messageTextView.backgroundColor = [UIColor clearColor];
    messageTextView.clipsToBounds = NO;  
    messageTextView.font = [UIFont fontWithName:@"Helvetica" size:14];

    textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];
    textViewImageView.image = [UIImage imageNamed: @"textbg.png"];
    textViewImageView.contentMode    = UIViewContentModeScaleToFill;
    textViewImageView.contentStretch = CGRectMake(0.5, 0.5, 0, 0);

    [messageTextView addSubview: textViewImageView];
    [messageTextView sendSubviewToBack: textViewImageView];
    [messageTextView addSubview:textViewLabel];
    [self.view addSubview:messageTextView];


-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
        CGRect frame = messageTextView.frame;
        frame.size.height = messageTextView.contentSize.height;

        messageTextView.frame = CGRectMake(35, 5, 210, frame.size.height);
        textViewImageView.frame = CGRectMake(0, 0, 210, frame.size.height);
}

当文本进入下一行时,您能否请任何人帮助解决增加 UIImageview 高度的问题?提前致谢。

【问题讨论】:

  • 如何向 UITextView 添加子视图?
  • @luyuan 好的,有什么方法可以跟踪输入到 UITextView 下一行的文本吗?请帮帮我。
  • 您的代码似乎没问题,因为两个框架的高度相同。但是为什么不尝试将 messageTextView 和 textViewImageView 添加到同一个视图中,并为它们设置完全相同的框架呢?
  • 当用户输入文本并且文本到达宽度的末尾时,一些字母会在那个时候图像视图框架高度较小并且文本显示在图像之外。你能帮我么?如果文本到达 textview 的末尾,有什么方法可以将文本中断到下一行?

标签: iphone ios uiimageview uitextview frame


【解决方案1】:

改变

 [messageTextView addSubview: textViewImageView];

 textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];

 textViewImageView.frame = CGRectMake(0, 0, 210, frame.size.height);

进入

 [messageTextView.superview addSubview: textViewImageView];

 textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(35, 5, 210, 30)];

 textViewImageView.frame = CGRectMake(35, 5, 210, frame.size.height);

正如我刚才测试的那样,它工作正常。

编辑 2 在我的测试中。

TestViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController <UITextViewDelegate>{

}

@property (nonatomic, strong) IBOutlet UITextView *textView;
@property (nonatomic, strong) IBOutlet UIImageView *imageView;


@end

TestViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textView.delegate = self;

    // Do any additional setup after loading the view, typically from a nib.
}

-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    CGRect frame = self.textView.frame;
    frame.size.height = self.textView.contentSize.height;

    self.textView.frame = frame;
    self.imageView.frame = frame;

    return YES;
}

【讨论】:

  • 当我添加你的代码时,textview 看起来像这样。请看我的问题。我将上传另一张有问题的图片。谢谢。
  • 我的应用程序面临第二个图像问题。你能给出一些指导方针来解决这个问题吗?谢谢。
  • @Gopinath 我在我的答案中上传了一个屏幕截图。这就是你想要的吗?
  • 是的,我想要那样。当长文本到达 UITextview 的边缘时,我遇到了问题。如果您不介意,请用您的答案编辑我的问题。这样我就可以很容易地找到它。对不起,打扰了。谢谢。
  • 谢谢。我将结合您的想法并让您知道解决方案。再次感谢。
猜你喜欢
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2011-01-11
  • 2016-01-21
  • 1970-01-01
相关资源
最近更新 更多