ルールド ノートブック ビューを作成しようとしています。
これを作成するコードは以下のとおりです
@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 の横罫線と左側の縦余白線を正しく描画します。
私がやろうとしている最後のことは、テキストビューのテキストを右にシフトして、垂直線の右側に配置することです。
何か案は?