Core Text - How to grow UIView? - iphone

I am using Core Text (for text bleed/wrap effect). Since the body of the UIView has to grow with the text and images put onto the view, I want my UIView to adjust its height accordingly.
I tried sizeToFit (on my UIView) to adjust its height according to its contents. However, the UIView doesn't increase its height, although it can decrease it alright.
This is how my hierarchy of the UIView looks like:
[UITextView]
[UIImageView] (can be 0 or many)
How could I make my UIView adjust its height according to the contents it has?

I am putting the text in a UITextView and then displaying it in the UIView (using CoreText), so I basically set the height of my UIView as:
(height of textview) + (height of images)
Although it isn't an elegant solution but atleast it works alright.
PS: a better solution might be to add a right proportion of height and width (since the text wraps around the image).

Related

UIImageView masks and bottom alignment

I am working on an app that has TableViewCells with varying heights. I am placing a UIImageView into them and each will use the same size image (the same size of the largest cell), however I need to mask the excess in the smaller cells (keeping the bottom, not the top).
To be more specific, I have 3 different cell heights, 112, 104 and 88. The images will all be 112 tall and I want the images to have the tops cut off on the smaller cells. Im pretty sure the answer lies within the bounds, frame and center attributes of a UIImageView, but I cant figure out exactly what I should be doing.
You will need to set the frame of your image view such that the bottom of the image view is aligned with the bottom of your cell.
You will also need to ensure the contentview of you cell is clipping content to its bounds (setClipToBounds to yes).
If I had to do this I would subclass the UITabelViewCell class and implement the layoutSubviews method. In your implementation don't forget to call super first so the content view has the right size (also if you go in editing mode). Then use the content view bounds and place your content accordingly.
When you add the image view to the cell, it should automatically cut off the top part, as long as you have the UIImageView bottom positioned within the frame, it should be what you wanted.

dynamic height for UIScrollView

I have a UIScrollView inside my UITableViewCell. I have a NSAttributeString inside this displayed (I am using this framework. Basically what I want to do is to adjust the size of the cells and the UIScrollView as well.
If the text in the NSAttributeString can fit in a UIScrollView less than 500, then the UIScrollView gets that height. Otherwise let the UIScrollView height's be 500.
Now the problem is that it gets more complicated than that. The NSAttributeString is an HTML and so it's hard to estimate the height of the row just by treating the HTML as strings as it can have image etc. So is there a way to go around this, so I can set the height of each row dynamically according to the content of the UIScrollView...
I was also looking for this kind of thing. Someone suggests me to get height using UIWebView when page loads get heigh by some javascript. Here is the original question
How to calculate height of html string?

Dynamically set UIView size depending on height of grouped table view

Setup: I have a UIView with a scroll view nested within it. Within the scroll view I have a label, uiimage, and a tableview (grouped). The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).
Expected: The uiview should extend in height depending on the height tableview extends to. Then I will be able to set the scrollview to accommodate the height I need to be able to scroll.
Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in almost any scenario.
Am I using UIScrollView incorrectly? Do I need to resize the UIView at all?
You don't have to modify your UIView frame size, which has to be the size of your screen. The UIScrollView frame size must also be the same, it represents the part of its view actually displayed.
What must change is the UIScrollView contentSize, which defines height and width for data inside it ;)
You can calculate it using each inside element's height and by adding the correct margin.
Thus, you could have a UIScrollView content size of 320 * 600, which will let you automatically scroll down.
In fact, you have to display your content independently of the final frame size. If you have a content of 500*500, just display it inside your UIScrollView. Then tell it the size of it's content, and it will automatically set scrolling possibilities if needed.
Turns out I had to create a UIView programmatically and set it as the header of the UITableView. It works perfectly now. :)

UIScrollView autoresizing content?

If I set UIScrollView height to self.view.frame.size.height and setAutoresizingMask:UIViewAutoresizingFlexibleHeight then when the view loads the UITabBarController and the UINavigationBar the scroll view frame resizes to suit.
However the content size (set at: self.view.frame.size.height) doesn't change. Fair enough I guess - i can't seem to find an autoresizing option for that, so why would it autoresize.
My fix is to just set the content size as less than the frame height, thus the content will always be the height of the frame. (eg: set content size height to 10points).
I'm just wondering if this is the best way to go about this, or if there's a better way? I'm doing this code in viewDidLoad.
Thanks
Tom
The UIScrollView's contentSize property define the size of the view displayed for Span&Zoom. It normally does not have any relation with the container (i.e. scrollview) size but if you really need to resize the content according to the scrollview size, you can overload the layoutSubViews method to do so.

issue with rotation and uilabels with size set using NSString's sizeWithFont

I have a few UILabels in my view with their height set using sizeWithFont:.
I set the autoreszingMask to flexible width and height, however on rotate the width changes (am assuming because self.view's width changes and its set with a width relative to self.view) but the height of it doesn't change to fit the content again. This results in a large white space at the top and bottom.
Just wondering how I would go about re-sizing the UILabel on rotation? Is there any automatic way of doing this, or is there a way of getting all UILabels and re-doing sizeWithFont when the device is rotated?
The UILabels are subviews of UIView heirarchies used as headers for sections in my UITableView.
Thanks
Tom
You should really send sizeToFit to any UIView, especially UILabel, instead of forcing some arbitrary size on them.
The best approach is usually to first set the maximum width and current height, then send sizeToFit, then adjust the width again if necessary. In case of multiline labels it looks like the most foolproof way to avoid layout bugs.
The problem was having a flexible left and right margin on a slightly offset uilabel.
If the label isn't centered then you need to choose left or right and set the flexible margin for one of those.