2012-12-30 36 views
1

好吧,我想创建一个统治笔记本视图。UITextview:如何缩进文字?

创建这个代码是下面

@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

+3

为什么不将它向右移动二十个像素? – 2012-12-30 19:11:27

+0

因为然后你得到一个20像素的差距,因为水平线已经移动了20个像素 – totalitarian

+3

解决方案:使背景和文本视图两个单独的视图。 – 2012-12-30 19:22:05

回答

0

OK我用contentInset 20个像素推文本向右解决了这个问题,然后修改了我的drawRect -20至延长水平线占contentInset