2013-04-04 3 views
1

Je souhaite tracer une ligne dans mon UITextView. Après quelques recherches, je trouve ceci:Ligne de dessin dans UITextView

UITextView ruled line background but wrong line height

J'ai essayé dans mon code, drawRect est appelé, mais pas de ligne est tirée .. Quelqu'un pourrait me aider?

#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 

Probablement une erreur stupide, mais je ne peux pas le trouver sur

Merci

Répondre

1

Comme je vous l'ai regardé coder tout est ok avec elle. Peut-être qu'il y a quelque chose avec de la couleur? Peut être que c'est la même chose que la couleur de fond ou quelque chose comme ça.

Peut être vous avez oublié de définir le mode contenu:

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

EDIT:

Je pense que je sais: Vous avez oublié de mettre [myTextView setNeedsDisplay]; après le montrer. Read here

EDIT:

D'abord, vous créez votre point de vue:

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

Faites votre viewcontroller mettre en œuvre UITextViewDelegate et après que

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

Cela devrait fonctionner

+0

J'essaie [myTextView setNeedsDisplay]; dans viewDidLoad et dans drawRect mais toujours pas de ligne :( – user2121458

+0

J'ai également utilisé ce code dans mon programme et tout est ok avec la méthode drawrect, je vais regarder plus de –

+0

Regardez mon edit –

0

Est-il possible que vous dessinez avec succès, mais s'obscurci par une couleur d'arrière-plan correspondant? Je remarque que l'alpha de la couleur que vous avez choisie est assez petit. Si seulement pour exclure cette, changer:

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

à

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

ne fonctionne pas , toujours pas de ligne: p – user2121458

1

J'ai même problème .. Comme je pense que vous n'êtes pas de sous-classement UITextView

Comme UIViewController ne met pas en œuvre cette méthode ne UITextView a

donc la sous-classe juste .. votre code fonctionnera