0

我想更改 TextView 中的光标位置...

在此处输入图像描述

在此处输入图像描述

// NoteView 目标 _c 类,它的超类是 TextView...

    #import "NoteView.h"

    @implementation NoteView

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code

           self.backgroundColor =[UIColor whiteColor];
    self.contentMode = UIViewContentModeRedraw;
            self.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
        }
        return self;
    }


    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {

        //Get the current drawing context
        CGContextRef context = UIGraphicsGetCurrentContext();
        //Set the line color and width
    //    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);

        CGContextSetStrokeColorWithColor(context,[UIColor colorWithRed:0.29804f green:0.12157f blue:0.9f alpha:0.1].CGColor);

        //Start a new Path
        CGContextBeginPath(context);

        //Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
        NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;

        //Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
        CGFloat baselineOffset = 6.0f;

        //iterate over numberOfLines and draw each line
        for (int x = 0; x < numberOfLines; x++) {
            //0.5f offset lines up line with pixel boundary
            CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x +     0.5f + baselineOffset);
            CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
        }

        //Close our Path and Stroke (draw) it
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }


     //its a view controller class and here i imported NotesView here
    #import "NotesViewController.h"

    @interface NotesViewController ()

    @end

    @implementation NotesViewController


// i load the textView frame  whatever i created class above
    - (void)loadView {
        [super loadView];
      CGSize  result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width*scale, result.height * scale);
if (result.height==1136)
{
    _TextView = [[NoteView alloc] initWithFrame:CGRectMake(29,50,266,430)];
    backgroundView.frame=CGRectMake(10,32,300,450);

}
else
{
    _TextView = [[NoteView alloc] initWithFrame:CGRectMake(29,50,266,340)];
    backgroundView.frame=CGRectMake(10,32,300,360);


}
[self.view addSubview:_TextView];
_TextView.delegate = self;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(30,40,1,backgroundView.frame.size.height-5)];
lineView.backgroundColor = [UIColor brownColor];
lineView.alpha=0.3;
[self.view addSubview:lineView];
UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(32,40,1,backgroundView.frame.size.height-5)];
lineView1.backgroundColor = [UIColor brownColor];
lineView1.alpha=0.3;
[self.view addSubview:lineView1];

}

    }

    }
// in view did load i set the delegate and code for cursor position
    - (void)viewDidLoad
    {
        [super viewDidLoad];

        // setting delegate here
        _TextView.delegate=self;
        _TextView.editable = YES;    
       [_TextView setSelectedRange:NSMakeRange(10, 0)];
    }

我为Textview的Textview和ScrollView编写了所有委托方法我光标位置是在垂直线之后开始的......我表达得不好请根据图像理解......我想要光标位置......我想要在 iPhone 中使用真正的笔记应用程序设置我的 Textview .. 我将 Textview 添加到 ViewController 一切正常,但光标统计起始位置...我想光标位置始终为水平线........尝试根据图片..

4

1 回答 1

0

像这张图片一样做。

在此处输入图像描述

在这里,您必须在 VC 中设置 textview 的位置,使其位于水平线之后。

于 2013-03-13T15:07:25.977 回答