1

好的,我正在尝试创建一个规则的笔记本视图。

创建它的代码如下

@interface QuickNoteNoteTextView ()
    @property (nonatomic) CGPoint startPoint;
    @property (nonatomic) CGPoint endPoint;
    @property (nonatomic, retain) UIColor *lineColor;
@end

@implementation QuickNoteNoteTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
 - (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:NSLineBreakByWordWrapping ];
     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:NSLineBreakByWordWrapping ];
     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, 0, 0, 0, 1);
     for(int i=offset;i < contentHeight;i+=height) {
         CGPoint lpoints[2] = { CGPointMake(0, i), CGPointMake(rect.size.width, i) };
         CGContextStrokeLineSegments(ctx, lpoints, 2);
     }

     //vertical line
     self.startPoint = CGPointMake(22.0, self.frame.size.height * -1);
     self.endPoint = CGPointMake(22.0, self.frame.size.height * 2);
     self.lineColor = [UIColor redColor];


     CGContextSetShouldAntialias(ctx, NO);

     CGContextSetStrokeColorWithColor(ctx, [self.lineColor CGColor]);

     CGContextSetLineWidth(ctx, 1);

     CGContextBeginPath(ctx);
     CGContextMoveToPoint(ctx, self.startPoint.x, self.startPoint.y);
     CGContextAddLineToPoint(ctx, self.endPoint.x, self.endPoint.y);
     CGContextMoveToPoint(ctx, self.startPoint.x + 2.0f, self.startPoint.y);
     CGContextAddLineToPoint(ctx, self.endPoint.x + 2.0f, self.endPoint.y);

     CGContextDrawPath(ctx, kCGPathStroke);


 }

此代码正确地在 UITextview 中绘制水平格线并在左侧绘制垂直边距线。

我要做的最后一件事是将文本视图中的文本向右移动,使其位于垂直线的右侧。

有任何想法吗?

在此处输入图像描述

4

1 回答 1

0

好的,我通过使用 contentInset 将文本向右推 20 像素解决了这个问题,然后修改了我的 drawRect 以将水平线延长 -20 以说明 contentInset

于 2012-12-30T19:50:12.573 回答