49

I have a UITextView. I want its text to start from top but I it is not coming from top. Please have a look at my code :

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;
} 

Please see the below screenshot.

UITextView is on the right side.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263

12 Answers12

52

Set the Content Inset like this in your UITextView

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

Adjust the Top value the way you want. this shoud fix your problem.

EDIT:

If you're having issues with iOS7 or above, try using...

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

Damitha Raveendra
  • 1,721
  • 17
  • 24
47

in the viewDidLoad method add

self.automaticallyAdjustsScrollViewInsets = false
masouk
  • 576
  • 4
  • 8
24

I did this way. When scroll disable, the the text loaded from top. After load than enable scroll.

- (void)viewDidLoad{
myTextView.scrollEnabled = NO;  
}       
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myTextView.scrollEnabled = YES;
}
22

None of the existing solutions helped, but this helped:

Objective-C:

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

Swift 3:

override func viewDidLayoutSubviews() {
    textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
Tung Fam
  • 7,899
  • 4
  • 56
  • 63
4

This is what I use

textView.textContainerInset = 
    UIEdgeInsetsMake(
        0,
        -textView.textContainer.lineFragmentPadding, 
        0, 
        -textView.textContainer.lineFragmentPadding
    )
;
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
3
self.yourTextView.scrollEnabled = NO;

self.yourTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • 2
    Code-Only answers are not considered as good answers. Please improve your answer by providing more context and explanations. – ckruczek Jul 04 '17 at 12:26
  • 1
    please write some explanation along with the code. code only answers are generally discouraged in SO --Review – Ram Ghadiyaram May 08 '19 at 19:12
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};
}
chandan
  • 2,453
  • 23
  • 31
0

Here is the answer

in Swift

termsTextView.contentOffset = CGPointMake(0, -220)

in Objective-C

[termsTextView setContentOffset: CGPointMake(0,-220) animated:NO];
Sameera Chathuranga
  • 3,638
  • 3
  • 27
  • 48
  • 1
    Your Swift code is not right version of Objective-C. It should be 'yourTextView.setContentOffset(CGPoint(x: 0, y: -220), animated: true)' – Saqib Omer Aug 04 '15 at 12:28
  • This is the correct code for me it worked! Well done Sameera – Radu Nov 11 '15 at 17:50
  • 4
    Suggested edit: `myTextView.contentOffset = CGPointMake(0, -myTextView.contentSize.height)` Rationale: The OP's UITextView is called `myTextView`, not `termsTextView`. And pulling the height dynamically makes the answer more useful to other readers (I hope), e.g. those that make UITextViews in the Interface Builder. – bjornte Jan 04 '16 at 10:52
0

This happens after I setting the autoresizingMask property of the UITextView in both iOS 7 and 8.

I found a optional workaround:

- (void)viewWillAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [yourTextView scrollRectToVisible:CGRectMake(0, 0, yourTextView.contentSize.width, 10) animated:NO];
}
liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
0

If you just want text views laid out in storyboards to load properly without adding view controller management, this'll do it (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
    }
}
Alex Curylo
  • 4,744
  • 1
  • 27
  • 37
0

swift 4 solution:

self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
levan
  • 440
  • 1
  • 8
  • 19
0

I just set the Y content offset to zero when I initialize the textview:

let textview: UITextView = .init(frame: .zero)
// initialize
textview.contentOffset.y=0
shoe
  • 952
  • 1
  • 20
  • 44