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 のすべてのデリゲート メソッドを記述しますカーソル位置は縦線の開始後です.... うまく表現されていませんが、画像に基づいて理解してください... カーソル位置が欲しい... したいTextview を iPhone の実際のメモ アプリケーションで設定します。ViewController に Textview を追加すると、すべてうまく機能しますが、カーソルの統計情報が開始位置になります。画像..

4

1 に答える 1

0

この画像のようにします。

ここに画像の説明を入力

ここでは、VC でテキストビューの位置を設定して、水平線の後に来るようにする必要があります。

于 2013-03-13T15:07:25.977 に答える