Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在适用于IPhone的UITextView上绘制直纹线_Iphone_Uitextview - Fatal编程技术网

在适用于IPhone的UITextView上绘制直纹线

在适用于IPhone的UITextView上绘制直纹线,iphone,uitextview,Iphone,Uitextview,我想在iPhone上创建一个类似notes应用程序的视图,因此需要该视图按照notes应用程序绘制线条,我在windows中完成了此操作,您需要获取字体度量,然后在设备上下文上绘制线条,是否有人在UITextView中执行此操作?如果有,请提供一些帮助您可以尝试使用带有规则线的图像设置文本视图的背景色 textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"RuledLinesPage.png

我想在iPhone上创建一个类似notes应用程序的视图,因此需要该视图按照notes应用程序绘制线条,我在windows中完成了此操作,您需要获取字体度量,然后在设备上下文上绘制线条,是否有人在UITextView中执行此操作?如果有,请提供一些帮助

您可以尝试使用带有规则线的图像设置文本视图的背景色

textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"RuledLinesPage.png"]];
如果要填充颜色的区域大于图像,则使用图案图像的颜色将创建平铺图像。因此,您必须确保图像大小正确/可平铺(我不认为“可平铺”是一个真正的词,但我希望您理解我的意思)。此外,您还必须创建带有规则线的图像,以最好地匹配textView的字体

祝你好运。

@lukya, 您的解决方案有点混乱,因为当我们滚动UITextView时,文本只会滚动,将行(来自图像)保留在原处


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

我认为这可以正常工作,但我觉得它已被黑客攻击,我不完全理解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);
    }
}
-(void)drawRect:(CGRect)rect{
//绘图代码
//获取图形上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
[超级drawRect:rect];
//获取单个文本行的高度
NSString*alpha=@“ABCD”;
CGSize textSize=[alpha sizeWithFont:self.font constrainedToSize:self.contentSize lineBreakMode:UILineBreakModeWordWrap];
NSU整数高度=textSize.height;
//获取视图或视图内容的高度,以较大者为准
textSize=[self.text sizeWithFont:self.font constrainedToSize:self.contentSize lineBreakMode:UILineBreakModeWordWrap];
NSUInteger contentHeight=(rect.size.height>textSize.height)?(NSUInteger)rect.size.height:textSize.height;
nsuiger offset=6+height;//将数字6从0偏移以获得第一行是否正常???
contentHeight+=偏移量;
//画直纹线
CGContextSetRGBStrokeColor(ctx、.8、.8、1);
for(int i=偏移;i
还担心这个神奇的数字6吗


Bob

子类UITextView。覆盖-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;
        }
    }
}

这并不完美。理想情况下,您应该只画入已传递的矩形。但我很懒,这是为了满足我的需要。

如果可行的话,那就聪明点。可以通过编程方式生成图像以适应字体。这应该是对答案的注释,而不是“答案”本身。您可以对此进行扩展吗?rowNumber是什么?对不起。当我复制和粘贴时,代码就留在那里了。我不相信这和这个代码有关。我已经删除了它。@DaveBatton什么是self.lineWidth??它是静态的还是动态的?self.lineWidth是一个公共的CGFloat属性。很好的解决方案。但是我添加了@Dave's
self.contentMode=UIViewContentModeRedraw
进入子类init方法,而不是
setNeedsDisplay
方法。
self.contentMode = UIViewContentModeRedraw;