UIScrollView dynamically resize UILabel - iphone

i have got a UILabel which is not static.
I want to resize my scroll view so that it fits the label.
Here is my idea now:
self.scrollView.contentSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
92 stands for pixels from where my label start. (there is a heading too)
But it doesn't work, it seems to be connected with Interface Builder also.
Thanks guys.

You need to set the frame as well. The contentSize of the scrollview is just what is within, not the frame of the view itself. If the contentSize is greater than the frame it will result in scrolling.
CGSize buttonSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
self.scrollView.contentSize = buttonSize;
self.scrollView.frame = CGSizeMake(0, 0, 320.0, buttonSize.width, buttonSize.height);

Related

UIScrollView Cutting off lables at the top of the screen

I am displaying multiple labels inside a UIScrollView, I can get them to display and I can get them to scroll, but a label at the top of the screen is cut off and is not visible unless you pull down. I have a tried adjusting the UIScrollViews content size, frame etc. all to no avail. Please help! Code:
[self.view addSubview: scrollView];
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
int scrollHeight = 1000;
scrollView.contentSize = CGSizeMake(scrollWidth,scrollHeight);
scrollView.frame = CGRectMake(0, 0, 320, 400);
Set the contentOffset to (0,0).

scrolling CoreText in UIScrollView

I am experimenting with CoreText, one of the problems i have is that content isn't scrollable and idk how to make it scrollable... with labels this code works:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [labelText sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeTailTruncation];
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
my test project: http://dl.dropbox.com/u/47384598/AA_CoreText.zip
but what should i do with CoreText? Any help highly appreciated!
You should probably render your CoreText frames into a UIView. Then, add this UIView to your UIScrollView as a subview and set the contentSize of the UIScrollView such that the contentSize is large enough to fit your UIView holding the CoreText.
Note that if the contentSize of the UIScrollView is less than the size of the view on the screen, it will not need to scroll.

Set starting point of content size for UIScrollView

How do I set the starting point of a UIScrollView? I would like to add a UIImageView left of the UIScrollView but changing the contentSize only adds scrolling room to the right of the scrollview. How do I add an ImageView left of the scrollView's (0,0) point and make it part of the scrollview's content size?
Hopefully I've got what you're trying to do here. I think this just takes a few turns with the contentOffset to get right.
Starting off;
Add the scrollView at frame (0,0,320,480) - its a full screen scroller
set contentSize to (320*3, 480) - it now has a content with the width of 3 'pages'
Add your imageView as a subview to the scrollView at frame (320,0,320,480)
set contentOffset of the scrollView to (320, 0) - this will move the content of the scrollView left, in the negative x direction by 320
Now your imageView will be on screen, but it will have a 320px width both on the left and right on the scroller content.
(Note that in the code below, i've simply added a UIView and not an imageView)
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.delegate = self;
scroller.pagingEnabled = YES;
scroller.backgroundColor = [UIColor blueColor];
scroller.contentSize = CGSizeMake(960, 480);
UIView *imgView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[imgView setBackgroundColor:[UIColor redColor]];
[scroller addSubview:imgView];
[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
Does that help?
Actually the best solution to start in the middle of the scroller if you are in a iPhone you should
[scroller setContentOffset:CGPointMake(320, 0)];
[self.view addSubview:scroller];
And for iPad
[scroller setContentOffset:CGPointMake(1024, 0)];
[self.view addSubview:scroller];
Try setting the Content offset.
[scrollView setContentOffset:CGPointMake(320, 0.0)];
If you're simply looking to set where the scroller content should be positioned at the start, you could do so with the - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method which will scroll the content to the point specified in the method.
Let me know if this is not what you wanted to know, and I'll get back to ya!
Cheers.
The key to setting the starting point, is to assign bounds with xy coordinates different than zero. Like this
lazy var contentView: UIView = {
let size = CGFloat(5000)
let view = UIView(frame: CGRectZero)
view.frame = CGRectMake(0, 0, size, size)
view.bounds = CGRectMake(-size/2, -size/2, size, size)
return view
}()
func centerContent() {
let frame = UIEdgeInsetsInsetRect(self.scrollView.bounds, self.scrollView.contentInset)
let navigationBarHeight = self.scrollView.contentInset.top
let x = (self.scrollView.contentSize.width/2) - (frame.size.width/2)
let y = (self.scrollView.contentSize.height/2) - (frame.size.height/2) - navigationBarHeight
self.scrollView.contentOffset = CGPointMake(x, y)
}

How to make UIScrollView respect the layout of the containing UIView?

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.
Here's the relevant code from the UIViewController:
- (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame];
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame];
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];
CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];
//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];
//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];
//turn on paging
[sv setPagingEnabled:YES];
UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];
}
Thank you in advance for any advice or help.
I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:
CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);
contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.
You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.
Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.
Instead, use any sane value for full frame and enable autoresizing:
CGRect fullFrame = {{0,0}, {320,480}};
...
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.

iPhone, how do I add a label so that text will be exactly center of the screen, vert and hotiz?

I've got a view which is shown in landscape mode and I want to add a label which is int he center of the view both horizontal and vertically.
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *lab = [[[UILabel alloc] initWithFrame:frame] autorelease];
lab.font = [UIFont boldSystemFontOfSize:16.0];
lab.textAlignment = UITextAlignmentCenter;
lab.text = #"No data!";
[self.view addSubview:lab];
However, its not the right width and its at the top of the view ?
The texts are vertically center aligned by default. If you make the frame height equal to super view (which covers whole window, I assume) height then it will be vertically center aligned in that view.
For horizontal alignment you need to set textAlignment property which you are already setting correctly. But you need to fix the frame width which should be 480.
CGRect frame = CGRectMake(0, 0, 480, 320);