7
votes

I have a UILabel inside a cell which contains several elements. I need the label to have attributed string which can fill the label's height, i.e. go into multiple lines if neccessary. I managed to achieve this and if I run the App on iOS7 it appears to be just fine(disregard the yellowish background color): enter image description here

Here is the setup of the UILabel:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@  %@", sender, content]];
    NSRange selectedRange = NSMakeRange(0, sender.length); // 4 characters, starting at index 22

    [string beginEditing];

    [string addAttribute:NSFontAttributeName
                   value:[AppereanceConfiguration fontMediumWithSize:18]
                   range:selectedRange];

    [string endEditing];

    self.notificationText.attributedText = string;

where self.notificationText is the UILabel that I'm talking about. In the xib file for the cell I've set the minimum font size to 3 and the number of lines to 0. As I said before, it works perfectly on iOS 7, but on iOS 6 for some reason it doesn't know how to do the word wrapping on it's own and it tries to "Truncate Tail" as this is the line break mode that has been set by default in the xib, resulting in a cell looking like this: enter image description here

If I change the line break mode to Word Wrapping it crashes the app on iOS 6 saying that:

NSAttributedString invalid for autoresizing, it must have a single spanning paragraph style (or none) with a non-wrapping lineBreakMode.

How do I get this to work on iOS 6?

3
Two comments. First, as I understand it, minimum font size does not work for UILabels with a numberoflines other than 1. Second, you may have to do the word wrapping on your own. Use boundingRectWithSize on your attributed string with an infinite (large) width. Then break the text into lines and add a carriage return where it needs them. Not fun, but possible. - Putz1103
I can't to this because the text is not static, i.e. it comes from an online service and I have no way of knowing where too break the lines.And the puzzling thing is that it works on iOS7 without using anything iOS 7 specific AFAIK. - damjandd
They must have changed the way NSATTributedStrings work in iOS7. So nothing specific, but new features in the background. And even with dynamic text you can format it any way you wish using the method I referred to. You get the text, you assign it to the label. You can format it prior to assigning it to the label (unless I'm not understanding how you populate the cell). - Putz1103
I'll try to explain it: This is part of a notifications screen, much like the facebook notifications. So when I get a notification it will show up in my app in a cell like this one, and populate the uilabel with the text that is passed inside the notification itself. So, I have no way of knowing the length or contents of the text beforehand in order to put line breaks in it. If your method works despite this, could you please elaborate a bit more on how to do it? - damjandd
You somehow create a cell. And somehow say something to the effect of cell.textLabel.text = notification.text. That is where you have the contents of the text. That is where you can modify it before pasting it into the cell. - Putz1103

3 Answers

14
votes

You can get it to work on iOS 6 by doing exactly what the compiler states. For whatever reason, you need to add a NSParagraphStyle attribute to your NSAttributedString in order for this to work on iOS 6.

You can do it like so:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
[YourMutableString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [YourMutableString length])];
3
votes

The problem was with the font I was using (a custom Helvetica font). I changed [AppereanceConfiguration fontMediumWithSize:18] for [UIFont boldSystemFontOfSize:16] and now it works. I guess that it had trouble calculating the necessary width because of the custom font.

1
votes

try this

    [self.notificationText setAdjustsFontSizeToFitWidth:NO];

maybe even only for iOS6

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { //*
       [self.notificationText setAdjustsFontSizeToFitWidth:NO];
}