2013-04-04 56 views
1

我想在我的UITextView中画线。经过一番研究,我发现这一点:在UITextView中画线

UITextView ruled line background but wrong line height

我在我的代码试过,drawRect中被称为,但没有绘制线条..有人可以帮助我在这里?

#import "FacebookViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface MyViewController(){ 
UITextView *text; 
} 

@end 

@implementation MyViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Do any additionnal customisation 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
text.contentMode = UIViewContentModeRedraw; 
//TextView init 
text = [[UITextView alloc]initWithFrame:CGRectMake(0,44,320,380)]; 
[self.view addSubview:text]; 
text.delegate = self; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (void)drawRect:(CGRect)rect { 
NSLog(@"TEST"); 
//Get the current drawing context 
CGContextRef context = UIGraphicsGetCurrentContext(); 
//Set the line color and width 
CGContextSetStrokeColorWithColor(context, [UIColor redColor].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 = (text.contentSize.height + text.bounds.size.height)/text.font.leading; 

//Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.) 
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, text.bounds.origin.x, text.font.leading*x + 0.5f + baselineOffset); 
    CGContextAddLineToPoint(context, text.bounds.size.width, text.font.leading*x + 0.5f + baselineOffset); 
} 

CGContextClosePath(context); 
CGContextStrokePath(context); 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
[text setNeedsDisplay]; 
} 

@end 

可能是一个愚蠢的错误,但我不能找到它

感谢

回答

1

正如我看着你的代码一切都确定了它。也许有什么颜色?可能它与背景颜色或类似的东西是一样的。

可能是你忘了设置内容模式:

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.contentMode = UIViewContentModeRedraw; 
    } 
    return self; 
} 

编辑:

我想我知道: 你忘显示它后设置[myTextView setNeedsDisplay];Read here

编辑:

首先创建视图:

t = [[[MyTextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)] autorelease]; 
[self.view addSubview:t]; 
t.delegate = self; 

让您的视图控制器实现UITextViewDelegate,之后

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    [t setNeedsDisplay]; 
} 

这应该工作

+0

我尝试[myTextView setNeedsDisplay];在viewDidLoad和drawRect中,但仍然没有行:( – user2121458 2013-04-04 16:46:05

+0

我也在我的程序中使用了这段代码,并且使用了正确的方法,但我会看起来不止 – 2013-04-04 16:48:46

+0

看看我的编辑 – 2013-04-04 16:55:40

0

有没有可能是你成功的绘图,但通过匹配的背景颜色越来越模糊?我注意到你选择的颜色的alpha值非常小。如果仅仅是为了排除这一点,更改:

CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.5f alpha:0.15f].CGColor); 

CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
+0

不起作用,仍然没有行:p – user2121458 2013-04-04 16:42:30

1

我有同样的问题.. 因为我觉得你是不是从UITextView的

继承

由于UIViewController中未实现此方法只UITextView的那样

所以只要继承它..你的代码将工作