UITextview:如何缩进文本?

时间:2012-12-30 19:08:02

标签: objective-c

好的我正试图创建一个统治的笔记本视图。

创建它的代码在

之下
@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中正确绘制水平格线,在左侧绘制垂直边距线。

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

有什么想法吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

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