UILabel render text incorrectly in IOS7 - uilabel

I use following code to calculate the bound of a UILabel
CGRect bound = [lblName.text boundingRectWithSize:(CGSize){206, 99999}
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:stringAttributes
context:nil];
The UILabel is a embedded in a UIScrollView, which is a subview of UITableViewCell.
here what i got
I made a test which use a UILabel in a table cell, and a UILabel in UIScrollView separately, and results are as I expected
Note that all setting (font, line break mode etc) of UILabel are the same in all those case. The boundingRectWithSize returns same result in all those case, only difference is the way UILabel render the text.
What is the problem here? did i miss sometthing?
UPDATE: this happen only when i load UILabel from nib, if it is created programmatically, there is no problem. (my project is migrated from xcode 4 to xcode 5)

Try this:
bound.size.height += 1;
UPDATE:
According to Apple's document
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
So you might want to use this approach:
bound.size.height = ceil(bound.size.height);

I was seeing the same behavior with some of my labels, which looked fine in iOS 6, but in iOS 7 they had extra padding at the top and bottom as in your pictures.
Here's what I had to do to finally get it to layout correctly in viewDidLoad - works on both iOS 6 and 7.
self.someLabel.autoresizingMask = UIViewAutoresizingNone;
self.someLabel.frame = CGRectMake(
self.someLabel.frame.origin.x,
self.someLabel.frame.origin.y,
labelWidth, // define elsewhere if you're targeting different screen widths
self.someLabel.bounds.size.height);
[self.someLabel sizeToFit];

Related

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.

UITextView contentOffset not working on iOS 7

I have a UITextView in my app. I need to change it's content offset dynamically every time a new string is appended. The code bellow works fine on iOS 6 and earlier versions, but not on iOS 7.
TextViewText.text = [TextViewText.text stringByReplacingCharactersInRange:RecentWordRange withString:string];
newStringLen = string.length;
[TextViewText setSelectedRange: NSMakeRange(RecentWordRange.location+string.length, 0)];
[TextViewText setContentOffset: CGPointMake(0,0) animated:NO];
[TextViewText setContentOffset:contentOffset animated:YES];
what is change in IOS7 for setContentOffset?
Use UITextView's textContainerInset for what would be insets, and use (UIEdgeInsets)contentInset for what would be (CGPoint)contentOffset.
No idea why someone decided this was the best way to handle UITextView offsetting, but this is working for me.
Also note that there is a strange bug that doesn't accept insets on the textView unless there is a right and left offset. So make sure left/right UIEdgeInsets are greater than 1.
Recently I resolved a similar problem in iOS 8. I Replaced text in the text view and it jumped around. Using the UITextRange related method helped.
UITextRange *RecentWordRange = TextViewText.selectedTextRange;
[TextViewText replaceRange:RecentWordRange withText:string];

Setting UITextView frame to content size no longer works in Xcode 5

The code snippet below worked to resize a UITextView frame to it's content height, before installing Xcode 5 but it doesn't work since the upgrade:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
I've searched and haven't found the fix. Any thoughts?
There's new stuff for this on iOS 7.
To get the "fitted" size used by the text view after it's updated its text, call usedRectForTextContainer: on the textView's layoutManager property, passing the textView's textContainer property as an argument.
Word of warning about scrolling: Be advised, though, that changing the frame size of a text view after it has updated it's text can have unexpected visual bugs if scrolling is disabled on your text view. If this happens, set scrolling enabled before editing the text of the text view, then disabling it after it's updated (if you need scrolling to remain disabled).
To work in iOS 7 (Xcode 5), just:
Give the entire space to receive the text, by setting:
[myTextView setScrollEnabled:YES];
Pass the real text:
myTextView.text = theTextVariable; or myTextView.text = #"The text...";
Autoresize textView:
[myTextView sizeToFit];
Disable scroll:
[myTextView setScrollEnabled:NO];
P.S: myTextView can be use also as self.myTextView or _myTextView
And have fun!
I believe the correct way to force a textView to update its contentSize is by calling
[textView layoutIfNeeded]
However, in iOS 7.0 and 7.1 this seems still not to work reliably unless you first set
textView.layoutManager.allowsNonContiguousLayout = false;
It's not clear to me whether this is a bug or not since I can't really find a good explanation of what "non-contiguous layout" even means.
(My personal use case is updating textView.text = newValue programmatically, then trying to resize the textView appropriately.)
[textView sizeToFit];
Is what you need.
All you need to do is make sure that:
[textView setScrollEnabled:YES];
BEFORE you set the UITextView text content.
You can then:
[textView sizeToFit];
[textView setScrollEnabled:NO];
After you've set the text. Same as writing your own bling function or employing complicated bounding rect methods. Why use something so complicated when the solution is as simple as three lines?
That said, wrap those functions like so:
- (void) setText:(NSString *)theTextToAdd andResizeTheDamnTextView:(UITextView *)textView {
[textView setScrollEnabled:YES];
[textView setText:theTextToAdd];
[textView sizeToFit];
[textView setScrollEnabled:NO];
}
And define it in a per-file or global basis to avoid having to manually write or copy/paste the four lines and call it every time. Just call it as:
[yourTextViewIvar setText:#"DUMMY STRING" andResizeTheDamnTextView:yourTextViewIvar];
If that doesn't work:
[yourTextViewIvar setText:[self setText:#"DUMMY STRING" andResizeTheDamnTextView:yourTextViewIvar]];
And you'll be golden.
I think..
That's Pseudocode. Just FYI.
A easier solution is use that:
[textViewExample sizeToFit];
This work for me.

How to make height of OHAttributedLabel scale with content height?

I use an OHAttributedLabel called demoLbl for displaying text with formatted areas. This label is laid out with Interface Builder and is connected to a property in my ViewController. After setting the attributedText to the label I want all the text to be displayed in the label.
If I don't resize the label then the text is cropped at the end of the label so the rest of the text is missing.
If I use [demoLbl sizeToFit]; then the height of the label is larger or smaller in height than the text (about 10 point, varying with the text's length) thus giving me blank areas at the bottom of my view (after scrolling) plus the width of the label is increased by about 2 points.
If I calculate the height of the original text (NSString) before putting it in a NSAttributedString and adding it to the label's attributedText property then the calculated height is way too small for setting it as the label's height.
Is there a hack or trick I can apply so that the label's height is adjusted according to the NSAttributedString's height?
PS: To be more specific I wanted to add OHAttributedLabel as a tag but it's not allowed to me yet.
I'm the author of OHattributedLabel.
I made some fixes recently about my computation of the size. Please check it out it will probably solve your issue.
I also added a method named sizeConstrainedToSize:fitRange: in NSAttributedString+Attributes.h that returns the CGSize of a given NSAttributedString (quite the same way UIKit's sizeWithFont:constrainedToSize: works, but for Attributed strings and CoreText and not plain stings an UIKit)
Actually OHAttributedLabel's sizeThatFits: calls this method itself now.
You can see if this category gives you a more reliable height.
https://gist.github.com/1071565
Usage
attrLabel.frame.size.height = [attrLabel.attributedString boundingHeightForWidth:attrLabel.frame.size.width];
I added this code to the implementation of the OHAttributedLabel class:
// Toni Soler - 02/09/2011
// Overridden of the UILabel::sizeToFit method
- (void)sizeToFit
{
// Do not call the standard method of the UILabel class, this resizes the frame incorrectly
//[super sizeToFit];
CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f);
CGRect frame = self.frame;
frame.size = [self sizeThatFits:constraint];
[self setFrame:frame];
}
// End Toni Soler - 02/09/2011
Thank you Olivier for sharing your code!

What is the best way to implement a variable-sized multi-line UITableCell?

I'm trying to display a table full of twitter statuses (yes, this is the Stanford Presence 2 assignment), which are variably sized. I can relatively easily determine the appropriate height for my rows with code that approximates (from accompanying lecture 9):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath
{
NSString *text = ...;
UIFont *font = [UIFont systemFontOfSize:...];
CGSize withinSize = CGSizeMake(tableView.width, 1000];
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + somePadding;
}
I have tried two approaches (and some tweaks to both) to get a multi-line word-wrapping field of text into my table row.
Add a UILabel as a subview to my custom UITableCell subclass, and set the numberOfLines property to either a calculated number based on the height above (say, 6), or to 0 (theoretically unlimited). The numberOfLines is ignored; I see either 1 or 2 lines, and no more.
Add a read-only UITextView as a subview. This has the problem that the UITextView eats my scrolling; I end up scrolling inside a UITextView row instead of moving smoothly from row to row. If I disable scrolling on the UITextView, I end up being unable to scroll at all.
This is a pretty common thing to do; what's the best way to accomplish it?
You might want to look at the userInteractionEnabled property of the UITextView. That should allow input to be passed through to the UITableView so you get scrolling.
Here's a link to a blog I posted on this subject. I used a UILabel with numberOfLInes = 0. I hope this will be of some help.
Sample Project with Variable Sized UITableViewCell
While playing with userInteractionEnabled=NO, scrollEnabled=NO, and getting the right autosizing parameters set in IB worked, I think that going with a UILabel with numberOfLines=0 and the same autosizing parameters is ultimately a better idea, for the next person.