48

私は持っていUITextViewます。テキストを上から開始したいのですが、上から来ていません。私のコードを見てください:

UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = rect;
myTextView.editable = NO;
myTextView.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE];
myTextView.backgroundColor = [UIColor clearColor];
myTextView.text = sourceNode.label;
myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
[cell.contentView addSubview:myTextView];
[myTextView sizeToFit];
[self alignTextToTopTextView:myTextView]; 

alignTextToTopTextView :

-(void)alignTextToTopTextView :(UITextView*)textView{

    CGRect frame = textView.frame;
    frame.size.height = textView.contentSize.height;
    textView.frame = frame;
} 

以下のスクリーンショットをご覧ください

UITextView右側にあります。

ここに画像の説明を入力

4

11 に答える 11

52

UITextView でこのように Content Inset を設定します

youtextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);

必要に応じてトップ値を調整します。これで問題が解決するはずです。

編集:

iOS7 以降で問題が発生している場合は、以下を使用してみてください。

[yourTextView setContentOffset: CGPointMake(x,y) アニメーション:BOOL];

于 2012-05-29T04:50:14.357 に答える
47

viewDidLoad メソッドに追加

self.automaticallyAdjustsScrollViewInsets = false
于 2014-11-05T03:43:21.927 に答える
24

私はこのようにしました。スクロールを無効にすると、テキストが上から読み込まれます。ロード後、スクロールを有効にします。

- (void)viewDidLoad{
myTextView.scrollEnabled = NO;  
}       
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myTextView.scrollEnabled = YES;
}
于 2016-02-23T22:44:34.803 に答える
0

これは、iOS 7 と 8 の両方で のautoresizingMaskプロパティを設定した後に発生します。UITextView

オプションの回避策を見つけました:

- (void)viewWillAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [yourTextView scrollRectToVisible:CGRectMake(0, 0, yourTextView.contentSize.width, 10) animated:NO];
}
于 2015-06-02T11:31:27.437 に答える
0
- (void) viewDidLoad {
   [textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
   [super viewDidLoad];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
   UITextView *tv = object;
   //Center vertical alignment
   //CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
   //topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
   //tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};

   //Bottom vertical alignment
   CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height);
    topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
    tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
于 2014-01-23T12:00:13.720 に答える
0

これが答えです

スウィフトで

termsTextView.contentOffset = CGPointMake(0, -220)

Objective-Cで

[termsTextView setContentOffset: CGPointMake(0,-220) animated:NO];
于 2015-02-01T15:02:15.160 に答える
0

迅速な 4 ソリューション:

self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
于 2019-05-14T18:57:22.747 に答える