UILabel text overrun in custom tableCell - objective-c

I have a weird problem in a custom UITableCell subClass.This custom cell has a pair of UILables that are multi-line and can contain varying amounts of text (up to 2000 characters). The custom tableCell has a layout method where I am calculating their frame's height using the code below, taking the device orientation into account:
if (isPortrait) {
presentationTextLabelSize = [presentationTextStr sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(portraitDescriptionWidth, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
presentationTextLabelRect = CGRectMake(60.0f, 25.0f, portraitDescriptionWidth, presentationTextLabelSize.height);
} else {
presentationTextLabelSize = [presentationTextStr sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(landscapeDescriptionWidth, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
presentationTextLabelRect = CGRectMake(60.0f, 25.0f, landscapeDescriptionWidth, presentationTextLabelSize.height);
}
self.presentationTextLabel.frame = presentationTextLabelRect;
I have the label's autoResizeMask set as follows:
self.presentationTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
I use the same calculation in the UITableViewController to set the row height.
Everything works perfectly, the label is sized to the correct height for it's width, and the cell autoResizes on device rotation, except that when the device is rotated to landscape the label's text sometimes extends out of the label's frame to the right, clear past the edge of the tableView. If I scroll the cell out of view and back again it's back to normal. I am logging the cell's width to the console, and it is correct, yet the text extends far past that width.
Any ideas what's joing on?
Thanks

The problem is, the tableView is not reloaded when the device is rotated to landscape. So, there is mismatch in updating the texts. Use [tableView reloadData]; at the place, where you find the device is changed to landscape mode.

Related

How to resize NSTextView

I want to resize NSTextView accordingly to it's content. Initially its height equals one line height and if there's more then one line of text, I want to resize it to three line height.
The problem is TextContainer size is not affected when I resize text view.
Here is screenshot to better illustrate the problem.
Text view has red background color to distinguish from scroll view, and definitely its size changed. As you can see, part of the text that is on the second line is not displayed fully as if text container were smaller than text view
Here is code I use for resizing:
-(void)unfold {
NSScrollView *scrollView = (NSScrollView *)self.superview.superview;
NSRect scrollFrame = scrollView.frame;
NSRect textFrame = self.frame;
scrollFrame.size.height=62;
scrollFrame.origin.y-=40;
textFrame.size.height=60;
self.superview.superview.frame = scrollFrame;
self.frame=textFrame;
}
I also tried to play around with text container setHeightTracksTextView or changing its size but it wouldn't work.

UIVIew from XIB with Autolayout to UItableView Header

I am writing because I have a problem with the Auto Layout.
I'm trying to create a simple view in InterfaceBuilder with Auto Layout I want to load code and enter as a header of a table (not as header section). I explain briefly what are the characteristics.
The imageView must be square and must be as wide as the screen.
The space under the picture to the bottom of view that contains the button and label must be high 50 points.
Between image and button has to be a fixed distance of 12 points.
Between image and label must be a fixed distance of 13 points.
All these features are able to get them with Auto Layout. I added a constraint to the aspect ratio of the image (1: 1) and the various constraints for distances. all right.
The real problem is that by launching the app on iphone 6+ simulator (414 points of width), the image (with the label and button) goes above the cells.
Enabling various transparencies I noticed that the superView of Image View, only increase the width. It does not increase its height! How do I fix?
This is the code:
- (void)viewDidLoad{
//...
PhotoDetailsHeaderView *hView = (PhotoDetailsHeaderView *)[[[NSBundle mainBundle] loadNibNamed:#"PhotoDetailsHeaderView" owner:self options:nil] objectAtIndex:0];
hView.delegate = self;
self.tableView.tableHeaderView = hView;
//...
}
This is how I create the xib:
and this is how it is on the simulator, the green box is Uiimageview and the yellow box (under green box) is the mainview (or superview):
How can fix it?
Many thanks to all!
You'll need to add a property to store your PhotoDetailsHeaderView:
#property (nonatomic, strong) PhotoDetailsHeaderView *headerView;
Then calculate its expected frame in viewDidLayoutSubviews. If it needs updating, update its frame and re-set the tableHeaderView property. This last step will force the tableView to adapt to the header's updated frame.
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
CGRect expectedFrame = CGRectMake(0.0,0.0,self.tableview.size.width,self.tableView.size.width + 50.0);
if (!CGRectEqualToRect(self.headerView.frame, expectedFrame)) {
self.headerView.frame = expectedFrame;
self.tableView.tableHeaderView = self.headerView;
}
}
The problem is probably that in iOS you have to reset the header of the table view manually (if it has changed its size). Try something along these lines:
CGRect newFrame = imageView.frame;
imageView.size.height = imageView.size.width;
imageView.frame = newFrame;
[self.tableView setTableHeaderView:imageView];
This code should be in -(void)viewDidLayoutSubviews method of your view controller.

How to get the padding from the edge of the UITableview to the UITableViewCell

On the iPad, the Grouped style tableview's cells are inset deeper from the edge of the tableview than on the iPhone.
I need to retrieve the Left and Right distances from the edges of the tableview to where the cell begins. What i'm referring to is similar to "Margins". I read the UITableview API up and down and can't find a property that returns this.
I need to use this in calculation to compute where to position content in my cells.
Thanks in advance!
Alex
I haven't tested this but i'm pretty sure you should just be able to pick up the frame of both and then compare from there.
CGRect cellFrame = yourCell.frame;
CGRect tableFrame = yourUITableView.frame;
The CGRect values are (x coordinate, y coordinate, width, height).
Also you can just print out the frames using :
NSLog(#"your cell frame is %#",NSStringFromCGRect(yourCell.frame);
NSLog(#"your table frame is %#",NSStringFromCGRect(yourUITableView.frame);
I solved this with overriding the layoutSubviews call for the iPad and setting the grouped view margins to what I want them to be, rather then what the apparently hidden value is. Other answers in Stack point out that it can vary from 10 to 45 pixels in width.
In your CustomTableViewCell class
- (void)layoutSubviews
{
CGRect f = self.bounds;
f = CGRectInset(f, 10, 0);
self.contentView.frame = f;
self.backgroundView.frame = f;
}
You could force it to keep contentView and backgroundView to be equal to that of the TableCell width which is that TableView width, but in this case I still wanted my grouped view to be inset a little bit. It also allows you to better match with a custom header/footer view which will go edge to edge without work.

UItableView Cell Content size for iPad

Hi I face the strange problem.
I have created UITableView for iPad.
When I check the cell width i found it's 320 but for iPad i need 768 and 1024.
The below screen shows the TableView in which the cell shows in Table
When I add scrollBar with following UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 250)];
NSLog(#"cell.contentView.frame.size.widt %f",cell.contentView.frame.size.width);
the scrollbar shows only 320 width(cell.contentView.frame.size.width) area.
what should i do for set the iPad size cell content view
You should use autoResizingFlags correctly. On creation, the cell is only 320px width, but on display it will be larger.
Try this, when creating your scrollview:
previewScrollView.autoresizingFlags = UIViewAutoresizingFlexibleWidth;
This should work, but when you even need more control, you can still use the tableView delegate method: tableView:willDisplayCell:atIndexPath:

SetFrame works on iPhone, but not on iPad. Auto resize mask to blame?

I'm trying to resize a UITextView when the keyboard shows. On iPhone it works beautifully. When the the system dispatches a keyboard notification, the text view resizes. When it's done editing, I resize it to fill in the initial space. (Yes, I'm assuming the keyboard is gone when the editing stops. I should change that. However, I don't think that's my issue.)
When I resize the textview on the iPad, the frame resizes correctly, but the app seems to reset the Y value of the frame to zero. Here's my code:
- (void) keyboardDidShowWithNotification:(NSNotification *)aNotification{
//
// If the content view being edited
// then show shrink it to fit above the keyboard.
//
if ([self.contentTextView isFirstResponder]) {
//
// Grab the keyboard size "meta data"
//
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//
// Calculate the amount of the view that the keyboard hides.
//
// Here we do some confusing math voodoo.
//
// Get the bottom of the screen, subtract that
// from the keyboard height, then take the
// difference and set that as the bottom inset
// of the content text view.
//
float screenHeightMinusBottom = self.contentTextView.frame.size.height + self.contentTextView.frame.origin.y;
float heightOfBottom = self.view.frame.size.height - screenHeightMinusBottom;
float insetAmount = kbSize.height - heightOfBottom;
//
// Don't stretch the text to reach the keyboard if it's shorter.
//
if (insetAmount < 0) {
return;
}
self.keyboardOverlapPortrait = insetAmount;
float initialOriginX = self.contentTextView.frame.origin.x;
float initialOriginY = self.contentTextView.frame.origin.y;
[self.contentTextView setFrame:CGRectMake(initialOriginX, initialOriginY, self.contentTextView.frame.size.width, self.contentTextView.frame.size.height-insetAmount)];
}
Why would this work on iPhone, and not work on iPad? Also, can my autoresize masks be making an unexpected change?
Like said #bandejapaisa, I found that the orientation was a problem, at least during my tests.
The first thing, is about the use of kbSize.height being misleading, because in Landscape orientation it represents the width of the keyboard. So, as your code is in a UIViewController you can use it this way:
float insetAmount = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?kbSize.height:kbSize.width) - heightOfBottom;
The self.interfaceOrientation gives the orientation of the Interface (can be different from the Device orientation) and the macro UIInterfaceOrientationIsPortrait returns YES if the given orientation is Portrait (top or bottom). So as the keyboard height is in the kbSize.height when the interface is Portrait, and in the kbSize.width when the interface is Landscape, we simply need to test the orientation to get the good value.
But that's not enough, cause I've discovered the same problem with the self.view.frame.size.height value. So I used the same workaround:
float heightOfBottom = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?self.view.frame.size.height:self.view.frame.size.width) - screenHeightMinusBottom;
Hope this helps...