在UITextField中点击返回键后出现文本

时间:2011-12-23 08:59:34

标签: iphone xcode ipad ios4 xcode4

我有一个自定义类来创建一个文本视图,其中包含来自Apple的Notes应用程序。

这是班级的样子:

NoteView.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NoteView : UITextView <UITextViewDelegate> {
}


@end

NoteView.m

#import "NoteView.h"

@implementation NoteView

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:0.6f alpha:1.0f];
        self.font = [UIFont fontWithName:@"Marker Felt" size:20];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    //Get the current drawing context   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    //Set the line color and width
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
    CGContextSetLineWidth(context, 1.0f);
    //Start a new Path
    CGContextBeginPath(context);

    //Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
    NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;

    //Set the line offset from the baseline.
    CGFloat baselineOffset = 6.0f;

    //Iterate over numberOfLines and draw each line
    for (int x = 0; x < numberOfLines; x++) {
        //0.5f offset lines up line with pixel boundary
        CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
        CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
    }

    //Close our Path and Stroke (draw) it
    CGContextClosePath(context);
    CGContextStrokePath(context);
}

@end

我将视图添加为子视图。一切正常,但文本只有在我点击返回键后才会出现。我可以看到闪烁的光标,当我输入时我可以看到光标移动,但是文本消失了......点击返回键后,文本变得可见,之后一切正常。即使我选择了所有文本,删除它并开始输入文本也是可见的。

这就是textarea的样子:

enter image description here

如何解决这个问题?

提前致谢!

更新:

这就是我分配视图的方式:

annotateText = [[NoteView alloc] initWithFrame:CGRectMake(85.0, 168.0, 600.0, 567.0)];
    annotateText.backgroundColor = [UIColor clearColor];
    [annotateText setText:[annotationNotes objectAtIndex:0]];

    [paperAnnotationView addSubview:annotateText];

1 个答案:

答案 0 :(得分:0)

我知道这个问题很老了。我遇到了同样的问题,正在寻找解决方案。所以我想我会在这里发布答案,以防万一,如果有人来找它。拉了几个小时后,我发现当文本视图的边界不在屏幕上时,文本视图不会在第一行显示文本(除非我们按回车/返回或滚动文本)。即使父视图不在屏幕上,我也遇到了同样的问题。

以下是我正在处理的代码。

    self.captionView = [[[UIImageView alloc] initWithFrame:CGRectMake(231, 769, 563, 643)] autorelease];  //63
    self.captionView.image = [UIImage imageNamed:@"ekp006_preview_fbsharecontainer"];
    self.captionView.userInteractionEnabled = YES;
    //self.captionView.layer.contents = (id)[UIImage imageNamed:@"ekp006_preview_fbsharecontainer"].CGImage;

    UIButton *cancelButton = [[[UIButton alloc] initWithFrame:CGRectMake(10, 10, 73, 34)] autorelease];
    [cancelButton setImage:[UIImage imageNamed:@"ekp006_preview_fbshare_btn_cancel"] forState:UIControlStateNormal];
    [cancelButton setImage:[UIImage imageNamed:@"ekp006_preview_fbshare_btn_cancel_down"] forState:UIControlStateHighlighted];
    [cancelButton addTarget:self action:@selector(cancelFacebookShare) forControlEvents:UIControlEventTouchUpInside];
    [self.captionView addSubview:cancelButton];

    UIButton *shareButton = [[[UIButton alloc] initWithFrame:CGRectMake(480, 10, 73, 34)] autorelease];
    [shareButton setImage:[UIImage imageNamed:@"ekp006_preview_fbshare_btn_share"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageNamed:@"ekp006_preview_fbshare_btn_share_down"] forState:UIControlStateHighlighted];
    [shareButton addTarget:self action:@selector(facebookshare) forControlEvents:UIControlEventTouchUpInside];
    [self.captionView addSubview:shareButton];

    self.captionTextView = [[[UITextView alloc] initWithFrame:CGRectMake(20, 60, 523, 100)] autorelease];
    self.captionTextView.textColor = [UIColor lightGrayColor];
    self.captionTextView.delegate = self;
    self.captionTextView.layer.shadowRadius = 5.0;
    self.captionTextView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.captionTextView.layer.shadowOpacity = 0.8;
    self.captionTextView.layer.borderColor = [UIColor blackColor].CGColor;
    self.captionTextView.layer.borderWidth = 0.75;
    self.captionTextView.layer.cornerRadius = 5.0f;
    self.captionTextView.font = [UIFont fontWithName:@"VAGRoundedBlackSSi" size:18];
    [self.captionView addSubview:self.captionTextView];

    [[self topMostViewController].view addSubview:self.captionView];

如果仔细观察,我的父视图(标题视图)的y源是769,我明确了这一点,这样我就可以使用UIView动画从下到上使视图滑动。为了解决这个问题,我不得不采用将父视图框架作为CGRectMake(231,63,563,643)的备用路径并隐藏整个视图。为了呈现它,我使用了这样的东西。

- (void) presentCaptionCaptureScreen
{
    // The code which works
    [AnimationEffects bounceAnimationForView:self.captionView afterTimeInterval:0];
    self.captionTextView.textColor = [UIColor lightGrayColor];
    self.captionTextView.text = @"Enter your caption for the photo";

    // The code which didn't work
/*
    [UIView animateWithDuration:0.7 animations:^
    {
        self.captionTextView.text = @"";
        self.captionView.frame = CGRectMake(231, 63, 563, 643);
    } completion:^(BOOL finished)
    {
        self.captionTextView.textColor = [UIColor lightGrayColor];
        self.captionTextView.text = @"Enter your caption for the photo";
    }];
     */
}

它对我有用!