1

I have set up a UIView with NSLayoutManager (TextKit) and the UIView is placed in a UIScrollView. Im having trouble resizing the UIView to the size of the text so that It can be scrolled.

Currently the text fills the screen but the scrollView doesn't really scroll to the rest of it.

UIView Code:

- (void)drawRect:(CGRect)rect {

[super drawRect:rect];


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
long value = [prefs integerForKey:@"Daven"];


if((int)value == 0){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Shacharit" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];


}else if((int)value == 1){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Mincha" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];


}else if((int)value == 2){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Maariv" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];


}else if((int)value == 3){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"ּBrachaAchrona" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];

}else if((int)value == 4){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"BirkatHamazon" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];

}else if((int)value == 5){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"TefillatHaderech" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];

}

attStorage = [[NSTextStorage alloc]initWithAttributedString:string];

textContainer = [[NSTextContainer alloc]
                                  initWithSize:CGSizeMake(self.frame.size.width, FLT_MAX)];

layoutManager = [[NSLayoutManager alloc]init];
[layoutManager addTextContainer:textContainer];
[attStorage addLayoutManager:layoutManager];

NSRange glyphRange = [layoutManager
                      glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: rect.origin];

}

Using

(void) [layoutManagerr glyphRangeForTextContainer:textContainerr];
    NSLog(@"WORK %f", [layoutManagerr
                       usedRectForTextContainer:textContainerr].size.height);

I get a number (I assume is the correct Height for text).

How can I use the number returned to resize my UIView.

In my ViewController.m I tried using

CoreDavening *coreDavening = [[CoreDavening alloc]init];
    self.daveningView.frame = CGRectMake(self.daveningView.frame.origin.x, self.daveningView.frame.origin.y, self.daveningView.frame.size.width,[coreDavening heightForStringDrawing]);
self.daveningLabel.contentSize = CGSizeMake(self.view.frame.size.width,[coreDavening heightForStringDrawing]);

Ps. The UIView is attached to the UIViewController as an IBOutlet and called daveningView but the UIView's Custom class name is CoreDavening. DaveningLabel is the ScrollView (I know its bad wording for a scrollView I plan to fix that)

EDIT 1: ViewController Code:

NO CONSTRAINTS.

self.daveningView.frame = CGRectMake(self.daveningView.frame.origin.x, self.daveningView.frame.origin.y, self.daveningView.frame.size.width, [coreDavening heightForStringDrawing]);

    self.daveningLabel.contentSize = CGSizeMake(self.view.frame.size.width,[coreDavening heightForStringDrawing]);
  • Scrollability of a UIScrollView is all about its `contentSize`. I don't see you setting that anywhere. In fact, I don't see any scroll view in your code at all. – matt Jun 05 '15 at 17:40
  • You seem to be implementing exactly what `UITextView` does. If you inspect its private hierarchy, it is a subclass of `UIScrollView` and has a `UIView` subview which is of the size of the layout area. Any reason why you are not using a text view? – Léo Natan Jun 05 '15 at 17:41
  • so @matt I edited the question (I forgot to add the code that adjusted the Content Size, my bad) Leo Natan: Yes there is a reason: Rendering this much hebrew text in UITextView is incredibly slow. Please refer to [Previous Question](http://stackoverflow.com/questions/30651858/uitextview-vs-uilabelscrollview/30654173?noredirect=1#comment49376259_30654173) to see what I mean..Why the down vote? – user2738258 Jun 05 '15 at 17:45
  • Are you using auto layout? – matt Jun 05 '15 at 17:54
  • yes The Scrollview takes up and resizes to the entire screen and so does the UIView (They resize together) – user2738258 Jun 05 '15 at 17:55
  • Well there's your problem. When you are using auto layout, you must use the special auto layout way of setting the content size. – matt Jun 05 '15 at 17:56
  • So how would I go about resizing so that I can scroll? Thanks – user2738258 Jun 05 '15 at 17:56
  • Basically what I'm saying is this: rip out your UIView and substitute a plain red tall UIView. When you've figured out how to scroll _that_, you will know how to scroll _your_ view. – matt Jun 05 '15 at 17:57
  • You cannot _set the frame_ (size, width, height) of _anything_ under autolayout. You must set the size of your view from the inside out using _constraints_. – matt Jun 05 '15 at 17:59
  • Since all I'm using is UIScrollView and UIview (and they take up the whole Screen) should I not use Constraints and just size them in the ViewDidLoad as (0,0,screenWidth,ScreenHeight) – user2738258 Jun 05 '15 at 18:02
  • 1
    That is a perfectly good solution too; in that case you can set the `contentSize` manually along with the frame of the view. - My book goes very deep into these matters, both with and without auto layout. There are a lot of strategies possible. You might want to inspect some of the examples: https://github.com/mattneub/Programming-iOS-Book-Examples The ones you want to examine are the ones starting at "bk2ch07p360scrollViewInCode" and proceeding through the examples of the scroll view in the nib without and with auto layout. – matt Jun 05 '15 at 18:06
  • My contentSize is resizing perfectly but my UIView isn't..See edit – user2738258 Jun 05 '15 at 18:16
  • Did you study the examples I pointed you to? I tell you again: this has nothing to do with layout managers. Solve the problem for a plain big red tall view in a scroll view. It's as simple as that. If you know how to give _it_ any height and make the scroll view scroll through the whole thing, you can do it for _any_ content view. – matt Jun 05 '15 at 18:47
  • 1
    By the way, there is a difference between "no constraints" and "no auto layout". – matt Jun 05 '15 at 18:51
  • 1
    I made you a little movie that shows how simple this is: https://youtu.be/L4RRUwoOZRY At the start of the movie, I "bounce" the scroll view to prove it has no height. Then I click the button to add a lot of text to the (previously empty) label inside the scroll view, and scroll to prove that we can show the whole label. – matt Jun 05 '15 at 19:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79815/discussion-between-user2738258-and-matt). – user2738258 Jun 05 '15 at 20:44
  • Ive tried looking at your tutorial and video. I just can't seem to get it – user2738258 Jun 05 '15 at 21:23

0 Answers0