2010-08-16 2 views
4

iPhone에서 메모 앱과 같은보기를 만들고 싶습니다. 메모 앱별로 선을 긋기 위해보기가 필요합니다. 필요한 창에서이 작업을 수행했습니다. 그래서 도움이iPhone 용 UITextView에 괘선 그리기

을 appriciated 될 경우 폰트 메트릭스는 다음 장치 컨텍스트 상에 선을 그릴, 사람이 UITextView에서 이런 짓을하고있다

답변

1

당신이 지배 라인

textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"RuledLinesPage.png"]]; 
와 이미지를 사용하여 텍스트 뷰의 backgroundColor로 설정을 시도 할 수 있습니다

패턴 이미지가있는 색상은 채우기 영역이 이미지보다 클 경우 타일 이미지를 만듭니다. 그래서 당신은 이미지 크기가 정확한 크기인지/tileable인지 확인해야 할 것입니다. (나는 'tileable'이 진짜 단어라고 생각하지 않지만 당신이 의미하는 바를 얻을 수 있기를 바랍니다.) 또한 textView의 글꼴과 가장 잘 일치하는 괘선으로 이미지를 만들어야합니다.

행운을 비네.

+1

작동하는 경우 똑똑합니다. 이미지는 글꼴을 프로그래밍 방식으로 생성 할 수 있습니다. – Justin

0

@lukya 당신의 솔루션은 우리가 UITextView를 스크롤 할 때 텍스트가 그 자리에있는 선들 (이미지에서 오는 부분)을 남기고 스크롤 할 때와 같이 조금 지저분합니다.

더 나은 해결책은 선을 그린 텍스트 뷰에 하위 뷰를 추가하는 것입니다. 텍스트 크기에 대한 변경 내용을 텍스트 증가/감소로 추적하려면 텍스트 뷰에 옵저버를 추가해야합니다.

+0

이것은 답변에 대한 주석이어야하며 "응답"자체는 아닙니다. – Santa

5

나는 이것이 정상적으로 작동한다고 생각하지만, 해킹 당했다고 생각하며, UITextView 클래스의 메커니즘을 완전히 undestand하지는 않는다.

먼저는 너무

- (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:UILineBreakModeWordWrap]; 
    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:UILineBreakModeWordWrap]; 
    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, .8, .8, .8, 1); 
    for(int i=offset;i < contentHeight;i+=height) { 
     CGPoint lpoints[2] = { CGPointMake(0, i), CGPointMake(rect.size.width, i) }; 
     CGContextStrokeLineSegments(ctx, lpoints, 2); 
    } 
} 

아직도이 매직 넘버 6

걱정 하위 클래스에서의 drawRect을 구현 한 후
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    // NSLog(@"scrollViewDidScroll The scroll offset is ---%f",scrollView.contentOffset.y); 
    [noteText setNeedsDisplay]; 
} 

스크롤에 다시 그리기를 강제로 위임에 다음을 추가해야합니다

Bob

+0

훌륭한 해결책. 그러나 @dave의'self.contentMode = UIViewContentModeRedraw;'를'setNeedsDisplay' 메소드 대신에 서브 클래스 init 메소드에 추가했습니다. – Reggian

8

하위 클래스 UITextView. -drawRect 오버라이드 (override) : 당신은 텍스트 뷰를 초기화하기 때

- (void)drawRect:(CGRect)rect 
{  
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 
    CGContextSetLineWidth(context, self.lineWidth); 
    CGFloat strokeOffset = (self.lineWidth/2); 

    CGFloat rowHeight = self.font.lineHeight; 
    if (rowHeight > 0) { 
     CGRect rowRect = CGRectMake(self.contentOffset.x, - self.bounds.size.height, self.contentSize.width, rowHeight); 
     while (rowRect.origin.y < (self.bounds.size.height + self.contentSize.height)) { 
      CGContextMoveToPoint(context, rowRect.origin.x + strokeOffset, rowRect.origin.y + strokeOffset); 
      CGContextAddLineToPoint(context, rowRect.origin.x + rowRect.size.width + strokeOffset, rowRect.origin.y + strokeOffset); 
      CGContextDrawPath(context, kCGPathStroke); 
      rowRect.origin.y += rowHeight; 
     } 
    } 
} 

는 UIViewContentModeRedraw에 contentMode를 설정해야합니다. 그렇지 않으면 텍스트와 함께 줄이 스크롤되지 않습니다.

self.contentMode = UIViewContentModeRedraw; 

이것은 완벽하지 않습니다. 이상적으로 통과 한 직사각형을 그릴 수 있습니다. 그러나 나는 게으르다. 이것은 나의 필요에 부응했다.

+0

이것을 확장 할 수 있습니까? 예 : rowNumber 무엇입니까? –

+0

죄송합니다. 복사하고 붙여 넣을 때 코드가 남아 있습니다. 이 코드와 관련이 있다고 생각하지 않습니다. 나는 그것을 제거했습니다. –

+0

@DaveBatton self.lineWidth는 무엇입니까 ?? 그것은 정적 또는 동적 ?? – Hitarth