2010-08-16 127 views
4

我想创建一个像iPhone上的笔记应用程序这样的视图,因此需要视图按照笔记应用程序划分线条,我已经在需要获取的窗口中完成此操作字体规格,然后绘制线条到设备上下文,有没有人在UITextView中做到了这一点,如果这样一些帮助将appriciated在iPhone的UITextView上绘制直线条

回答

1

你可以尝试设置的backgroundColor你的TextView使用图像与格线

textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"RuledLinesPage.png"]]; 

如果要填充颜色的区域大于图像,则带图案图像的颜色会创建平铺图像。所以你必须确保图像大小是正确的大小/花木(我不认为'花木'是一个真正的词,但我希望你明白我的意思)。此外,您将不得不使用直线创建图像以最好地匹配您的textView字体。

祝你好运。

+1

聪明,如果它的工作。该图像可以通过编程生成以适应字体。 – Justin 2010-08-16 10:32:50

0

@lukya, 因为当我们滚动UITextView时,你的解决方案有点麻烦,文本只滚动离开行(来自图像)在它的位置。

更好的解决方案是将子视图添加到您的文本视图中绘制线条。您需要在文本视图中添加观察者,以随着文本增加/减少来跟踪其内容大小的变化。

+0

这应该是对答案的评论,而不是“答案”本身。 – Santa 2010-08-18 06:16:33

5

我认为这个工作正常,但我觉得它已被黑客入侵,我没有完全摒弃UITextView类的机制;

首先你必须添加以下到您的委托来迫使滚动

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    // NSLog(@"scrollViewDidScroll The scroll offset is ---%f",scrollView.contentOffset.y); 
    [noteText setNeedsDisplay]; 
} 

然后在子类中实现的drawRect一个重绘所以

- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    // Get the graphics context 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    [super drawRect:rect]; 

    // Get the height of a single text line 
    NSString *alpha = @"ABCD"; 
    CGSize textSize = [alpha sizeWithFont:self.font constrainedToSize:self.contentSize lineBreakMode:UILineBreakModeWordWrap]; 
    NSUInteger height = textSize.height; 

    // Get the height of the view or contents of the view whichever is bigger 
    textSize = [self.text sizeWithFont:self.font constrainedToSize:self.contentSize lineBreakMode:UILineBreakModeWordWrap]; 
    NSUInteger contentHeight = (rect.size.height > textSize.height) ? (NSUInteger)rect.size.height : textSize.height; 

    NSUInteger offset = 6 + height; // MAGIC Number 6 to offset from 0 to get first line OK ??? 
    contentHeight += offset; 
    // Draw ruled lines 
    CGContextSetRGBStrokeColor(ctx, .8, .8, .8, 1); 
    for(int i=offset;i < contentHeight;i+=height) { 
     CGPoint lpoints[2] = { CGPointMake(0, i), CGPointMake(rect.size.width, i) }; 
     CGContextStrokeLineSegments(ctx, lpoints, 2); 
    } 
} 

还是担心这个神奇的数字6

Bob

+0

伟大的解决方案。但是我已经将@Dave的'self.contentMode = UIViewContentModeRedraw;'添加到子类init方法中,而不是'setNeedsDisplay'方法。 – Reggian 2012-05-23 22:13:27

8

子类UITextView。 Override -drawRect:

- (void)drawRect:(CGRect)rect 
{  
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 
    CGContextSetLineWidth(context, self.lineWidth); 
    CGFloat strokeOffset = (self.lineWidth/2); 

    CGFloat rowHeight = self.font.lineHeight; 
    if (rowHeight > 0) { 
     CGRect rowRect = CGRectMake(self.contentOffset.x, - self.bounds.size.height, self.contentSize.width, rowHeight); 
     while (rowRect.origin.y < (self.bounds.size.height + self.contentSize.height)) { 
      CGContextMoveToPoint(context, rowRect.origin.x + strokeOffset, rowRect.origin.y + strokeOffset); 
      CGContextAddLineToPoint(context, rowRect.origin.x + rowRect.size.width + strokeOffset, rowRect.origin.y + strokeOffset); 
      CGContextDrawPath(context, kCGPathStroke); 
      rowRect.origin.y += rowHeight; 
     } 
    } 
} 

当你初始化文本视图时,一定要将contentMode设置为UIViewContentModeRedraw。否则,这些行不会随文本一起滚动。

self.contentMode = UIViewContentModeRedraw; 

这并不完美。理想情况下,你应该画出通过的矩形。但我很懒,这符合我的需求。

+0

你可以在此扩展吗?例如。什么是rowNumber? – 2012-03-13 01:23:00

+0

对不起。当我复制并粘贴代码时,只剩下代码。我不相信这与这段代码有关。我已将其删除。 – 2013-05-07 18:32:48

+0

@DaveBatton什么是self.lineWidth?它是静态还是动态? – Hitarth 2013-06-04 06:33:12