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 回答
22

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

目标-C:

- (void)viewDidLayoutSubviews {
    [self.yourTextView setContentOffset:CGPointZero animated:NO];
}

斯威夫特 3

override func viewDidLayoutSubviews() {
    textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
于 2017-03-12T11:25:14.407 回答
4

这就是我使用的

textView.textContainerInset = 
    UIEdgeInsetsMake(
        0,
        -textView.textContainer.lineFragmentPadding, 
        0, 
        -textView.textContainer.lineFragmentPadding
    )
;
于 2017-04-30T13:43:09.940 回答
3
self.yourTextView.scrollEnabled = NO;

self.yourTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
于 2017-07-04T12:03:33.793 回答
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

这发生在我在 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

如果您只想在不添加视图控制器管理的情况下正确加载故事板中的文本视图,则可以这样做(iOS 12,Swift 4.2):

final class UITopLoadingTextView: UITextView {

    var shouldEnableScroll = false

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        shouldEnableScroll = isScrollEnabled
        self.isScrollEnabled = false
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        isScrollEnabled = shouldEnableScroll
    }
}
于 2019-03-06T02:36:06.163 回答
0

快速4解决方案:

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