Reputation: 143
I want to draw line in my UITextView. After some research I found this:
UITextView ruled line background but wrong line height
I tried in my code, drawRect is called but no line is drawn.. Someone could help me out here ?
#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
Probably a silly mistake but I can't find it out
Thanks
Upvotes: 1
Views: 3013
Reputation: 326
I have same problem .. As i think you are not subclassing from UITextView
As UIViewController does not implement this method only UITextView did
So just subclass it .. you code will work
Upvotes: 1
Reputation: 21726
As i looked to you code everything is ok with it. Maybe there is something with color? May be it is the same as background color or something like this.
May be you forgot to set content mode:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
EDIT:
I think i know:
You forgot to set [myTextView setNeedsDisplay];
after showing it. Read here
EDIT:
First you create your view:
t = [[[MyTextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)] autorelease];
[self.view addSubview:t];
t.delegate = self;
Make your viewcontroller implement UITextViewDelegate
and after that
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[t setNeedsDisplay];
}
This should work
Upvotes: 1
Reputation: 62686
Is it possible that you're drawing successfully but getting obscured by a matching background color? I notice that the alpha on the color you've chosen is pretty small. If only to rule this out, change:
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.5f alpha:0.15f].CGColor);
to
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
Upvotes: 0