Get Appropriate CGRect for an NSAttributedString - ios

As it is clear from the title I'm trying to find the appropriate size for bounds of a UILabel containing an NSAttributedString which contains some images using NSTextAttachment.
Before using the attributedText property of the UILabel, I was using text property which carries the plain text only, and the method I was using was
sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping
And it was working fine. But now I have changed the type of contents in my UILabel and this method no longer gives the correct size ( Because it does not consider width of images ).
I have Googled a lot and the only thing I did end up with for NSAttributedString was
boundingRectWithSize:CGSizeMake(220.f, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil
But it is not returning the appropriate size and most of the content is lost in UI using this method. So please either suggest another solution or if you are suggesting boundingRectWithSize please come up with clear instruction on how can I make it work.
Just in case it helps, What I'm trying to show is a text message that may contain smilies (Custom Images, not Emojis of course) and those smilies are stored and shown as NSTextAttachments.

Related

Confused with UIPickerView font size

I'm using UIPickerView but in a willing to custom the font size in a row in a more efficient way.
First I have tried viewForRow: to create a label by myself, and call titleForRow: in it to acquire the text content that need to show. It works, but I'm not sure if it is a proper way.
Then I delete the method viewForRow: and tried attributedTitleForRow: that successfully changed the text color using NSForegroundColorAttributeName, but failed to set font size using NSFontAttributeName by next line. I doubt if there is a constraint mechanism that forbid/autoresize the font size change in UIPickerView? (The UIPickerView actually have a constraint of height combines to 162, 180 or 216 that I can only use CGAffineTransformMakeScale to custom it, so I really doubt it has another constraints.)
Now I'm very confused if there is a way modify the font size without implement viewForRow:, thanks.
Okay~~~ Now I'm implementing the viewForRow: method.

Font Size resets in NSAttributedString

Am adding an image to a NSAttributedString object so I can display them properly in a UITextView.
So far, it works great. Only problem is, my Font size is 25 to match my image size but each time I add an image to the UITextView, the Font resets to some smaller size. I need to keep the font at 25 to match texts with the image size.
NSAttributedString has 3 constructors which take string, attributedString and string:attributes:.
Since I'm not using string, I can't set the attributes using the third constructor and the first 2 won't allow me to set attributes (UIFont). I therefore decided to set the Font size from Interface Builder and reset it each time I add an image to the `UITextField.
Am facing a problem here because each time I reset the font, the UITextView scrolls to the first line (assuming there is so much text, it has scrolled up). How do I set font size after adding image without the UITextView scrolling up automatically? Better still, is there a better way of doing this? Thanks.
NSMutableAttributed string has some methods you may try, including setting attributes across ranges after the attributed string is constructed.

TTTAttributedLabel not displaying the last line when having Emoji symbols

We are using "TTTAttributedLabel" for displaying labels. For calculating the correct rectangle size, we use NSString's "sizeWithFont" method, with a "constrainedToSize" the width of the field.
The calculation is fine, unless there are some Emoji symbols in the text, and the text is multi line (for example: smiley-newLine-smiley). In that case, the returned size is too small (vertically), and the last line is not shown. If the text does not contain any Emoji (e.g. X-newLine-X) - the size is correct.
Our font is "HelveticaNeue" size:16.25, in case is makes any difference.
Is there a better way to calculate the needed size, so that it will work with Emoji as well?
Thanks
I had a same situation when making auto-height label according to the contents of the label.
Everything seems fine, except when there are emojis in label content.
It was because I did't use the correct setText method for AttributedString.
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^
return mutableAttributedString;
}];
This is the correct way to set AttributedString, but what I did is
[label setAttributedText:text];
So it was getting wrong label heights when it includes emojis in it.
I solved this problem by changing this set method with the correct one as described in Github manual.

Sizing a UITableViewCell based on its contained UITextView

I'm working on an iOS app at the moment where I'm displaying lots of text in table view cells. The text in question is stored in an NSAttributedString can span an indefinite amount of lines and can have inline attachments such as images. The issue here is that I can't seem to get an accurate measure of a given cell's height to pass on to the table view. Is there a performant, simple way to calculate the height of an arbitrarily complex NSAttributedString contained in a UITextView?
You can call the boundingRectWithSize:options:context: method to find out its size.
[attributedString boundingRectWithSize:CGSizeMake(320.0f, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
You can substitute the 320.0f with whatever your expected width is for your text view.
Reference: NSAttributedString UIKit Additions Reference
As a footnote, NSString has similar categories outlined here.
I had many failed attempts at this until I came across the free Sensible TableView framework. The framework has what they call a TextViewCell that automatically resizes according to the text inside. Highly recommended.

Determine padding and size of iOS UITextView

Project
I am attempting to replicate the native iMessage app. I have used AcaniChat as a foundation.
I wanted automatic highlighting, so I modified the code to use UITextView instead of UILabel. I realize there are options such as FancyLabel and Three20. However, UITextView does this natively.
Problem
I am having a difficult time getting the padding/size of UITextView right. I updated the contentInset property based on suggestions in other answers.
msgText.contentInset = UIEdgeInsetsMake(-11.0f, -8.0f, 0.0f, -8.0f);
I am also determining the size with the following:
CGSize size = [[(Message *)object text] sizeWithFont:[UIFont systemFontOfSize:kMessageFontSize]
constrainedToSize:CGSizeMake(kMessageTextWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
Nonetheless, some text is still being cut off (see image below). Notably the phone number and email address (right) as well as the "that cut off?" (left)
I have verified this is not due to the dataDetectorsTypes property.
Question
I can solve this by increasing the CGRect of the UITextView. But I want to better understand the affects of margin/padding and size of the UITextView. I don't want to arbitrarily increase the size by 20.0f to make it work.
As such, what is the code or combination of code that I can reliably set the size of the message bubble?
Not a solution for your problems with UITextView but ...
I had similar problems and decided to give core text a chance. You have simply more control about what is happening.
You could have your string attributed using regex statements to make links/emails/phone numbers visible and have furthermore the chance to use even other fonts.

Resources