UITextView文本不是从顶部开始的

Nit*_*ish 45 iphone uitextview

我有一个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]; 
Run Code Online (Sandbox Code Playgroud)

alignTextToTopTextView:

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

    CGRect frame = textView.frame;
    frame.size.height = textView.contentSize.height;
    textView.frame = frame;
} 
Run Code Online (Sandbox Code Playgroud)

请参阅下面的屏幕截图.

UITextView 在右边.

在此输入图像描述

Dam*_*dra 48

在UITextView中设置这样的内容插入

youtextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Run Code Online (Sandbox Code Playgroud)

按您希望的方式调整Top值.这个应该解决你的问题.

编辑:

如果您遇到iOS7或更高版本的问题,请尝试使用...

[yourTextView setContentOffset:CGPointMake(x,y)animated:BOOL];

  • 看起来不错,但你知道如何计算顶部插图,而不是设置固定值吗? (5认同)

小智 48

在viewDidLoad方法中添加

self.automaticallyAdjustsScrollViewInsets = false
Run Code Online (Sandbox Code Playgroud)


小智 24

我这样做了.滚动禁用时,从顶部加载文本.加载后比启用滚动.

- (void)viewDidLoad{
myTextView.scrollEnabled = NO;  
}       
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myTextView.scrollEnabled = YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个解决方案.(带有swift3的iOS10.2) (2认同)

Tun*_*Fam 21

现有的解决方案都没有帮助,但这有助于:

Objective-C的:

- (void)viewDidLayoutSubviews {
    [self.yourTextView setContentOffset:CGPointZero animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

override func viewDidLayoutSubviews() {
    textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
Run Code Online (Sandbox Code Playgroud)