2013-10-21 85 views
0

的删除过程中获得淡入要在TextView的线编程我已经实现下面的代码:绘制的线条文字

- (void)drawRect:(CGRect)rect{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [super drawRect:rect]; 
    CGSize textSize = [alpha sizeWithFont:[UIFont fontWithName:@"FreeHand575" size:28.0] 
    constrainedToSize:self.contentSize lineBreakMode:NSLineBreakByCharWrapping]; 
    NSUInteger height = textSize.height; 
    textSize = [self.text sizeWithFont:self.font constrainedToSize:self.contentSize  
    lineBreakMode:NSLineBreakByCharWrapping]; 
    NSUInteger contentHeight = (rect.size.height > textSize.height) ? 
    (NSUInteger)rect.size.height : textSize.height; 
    NSUInteger offset = height; 
    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); 
    }} 

它可以正常使用。但是当用户从textview中删除文本并且某个地方画出的线条也会被删除或淡入时出现问题。

textview with fade lines

我无法找到问题。如果有人有一些想法,请帮助我。

在此先感谢

+0

你有没有试过写这行[super drawRect:rect];在最后? –

+0

@hussainShabbir是的我试过了,但没有解决问题 –

回答

0

你可以在主层的下面添加一个图层为TextView的,借鉴了线。这样绘图文本(或删除)不会影响行。 阅读here了解如何绘制图层,请参阅“CALayer和自定义绘制内容”。