2012-12-30 33 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