可以在UITextView iPhone中跟踪文本进入下一行吗?

时间:2012-07-20 14:55:09

标签: iphone ios uiimageview uitextview frame

我在UITextView for iPhone Messaging app工作。我在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高度的高度吗?提前谢谢。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

enter image description here

变化

 [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;
}