【问题标题】:Text appears after hitting the return key in a UITextField在 UITextField 中按回车键后出现文本
【发布时间】:2012-01-26 16:36:17
【问题描述】:

我有一个自定义类来创建一个文本视图,其中包含 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

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

这是文本区域的样子:

如何解决?

提前致谢!

更新:

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

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

【问题讨论】:

  • 您是否尝试明确设置文本颜色?
  • 首先检查您的代理设置是否正确。然后在不覆盖 drawRect 的情况下尝试它(以防万一发生冲突)。然后可能会发布代码,显示您在哪里/如何分配/初始化您的 NoteView。哦,向我们展示你的 shouldChangeCharactersInRange 方法。
  • 好吧,我需要重写 drawRect 来创建像我添加的图片中的线条。否则文本不会在线条上完美对齐......或者你有更好的主意吗?
  • 也试过设置文字颜色,没区别...
  • 你解决了吗?我一直无法找到解决方案。似乎是苹果方面的一个错误。

标签: iphone xcode ipad ios4 xcode4


【解决方案1】:

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

这是我正在处理的代码。

    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";
    }];
     */
}

它对我有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多