【发布时间】: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