13

I am trying to create a multiline UILabel with an NSMutableAttributedString. This works fine when I assign an attribute to the complete string like this:

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])];

contentLabel.attributedText = string;

But I what I need is to apply a number of attributes for different subranges of the NSAttributedString (to bold certain words).

UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 5)];

contentLabel.attributedText = string;

What I am finding is that if I do this, the text isn't rendered over multiple lines in the label anymore. It is rendered as a single line, centered vertically in the label's frame.

Is there something I'm missing here?

adriaan
  • 1,574
  • 14
  • 33
  • 1
    Did you make the label tall enough for the second line? – Lily Ballard Feb 27 '13 at 17:51
  • I can't reproduce this on 6.1. I note that your code is slightly incorrect (there's a missing ] in the setFont: line and you use "label" instead of "contentLabel" in one place). Are you sure this is the actual code? – Rob Napier Feb 27 '13 at 18:02
  • @Kevin Ballard: Yes, the label is tall enough. – adriaan Feb 27 '13 at 21:08
  • @Rob Napier: My bad, edited the code. I edited this code sample for presentation purposes - my actual code has a bit more going on, but this is the essence of where I have the issue. – adriaan Feb 27 '13 at 21:10
  • @RobNapier : You are right, the sample I gave actually does work. It appears my problem is elsewhere in my code. – adriaan Feb 27 '13 at 21:18
  • 3
    @KevinBallard : Turns out you were right, the required height I calculated for and assigned to the the label's frame was incorrect. Thanks for putting me on the right track. – adriaan Feb 27 '13 at 21:47
  • 1
    You should probably write your last comment down as an answer and accept it, this way other users (like me) will see it immediately. – gchbib Jul 30 '13 at 22:15

2 Answers2

4

As discussed in the question's comments, the code presented in the question actually works correctly, and does render the attributed string as intended. (The problem was elsewhere in my code)

adriaan
  • 1,574
  • 14
  • 33
1

I use frequently the UITextView and assign it a text attributed with a similar way:

        // creiamo textView descrizione
    UITextView *description = [[UITextView alloc] init];
    description.frame = CGRectMake(20, 453, 500, 190);

    NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                     NSBackgroundColorAttributeName: [UIColor clearColor],
                                     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:22.0],
                                     };

    NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                        NSBackgroundColorAttributeName: [UIColor clearColor],
                                        NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:10],
                                        };

    NSDictionary *attribute3 = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                           NSBackgroundColorAttributeName: [UIColor clearColor],
                                           NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]
                                           };

    NSMutableAttributedString *info = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ %@ \n\n%@ \n     ", string1, string2 , string3, string4]];

    [info setAttributes:attribute1 range:NSMakeRange(0, ([string1 length]))];
    [info setAttributes:attribute2 range:NSMakeRange(([string1 length]),([string2 length]+[string3 length]))];
    [info setAttributes:attribute3 range:NSMakeRange(([string1 length] [string2 length]+[string3 length]), [string4 length])];

    description.attributedText = info;
    description.backgroundColor = [UIColor clearColor];
    description.editable = NO;
    description.textColor = [UIColor blackColor];
    [viewInfo addSubview:description];
    [description sizeToFit];
Alessandro Pirovano
  • 2,509
  • 28
  • 21