2
votes

I'm having enough trouble with UILabel attributedText that I am now considering creating a UIView subclass that renders its attributed string manually in drawRect.

The gist of the problem I'm having is that UILabels defined in storyboard files always seem to render size 17.0 font at runtime. I've tried a number of hacks to fix this issue, but nothing has worked. No matter what I do, the label's font size remains at 17.0.

  1. The first thing I tried, which I considered to be the most natural solution, was to modify the font size directly in the storyboard. The in-storyboard preview of the label content updated appropriately, but at runtime the font had reset to size 17.0.

  2. The next thing I tried was to adjust the font size at runtime. Some interesting results were observed here: (a) when querying the font size, the label's attributedText property was reporting the correct font size, but it was still rendered as the default size 17.0. Setting the attributed text to something else had no effect. Furthermore, changing the attributed text font was unsuccessful (I tried a number of different things because I wasn't sure if it was just the size that was failing to set, but apparently everything under NSFontAttributeName was immutable.

  3. I also tried replacing the UILabel with a UITextView with user interaction disabled etc, because I thought this might be a bug specific to labels. Once again the text view's attributed text proved to be immutable at runtime, at least from a rendering perspective.

I've had a number of suspicions about what might be causing this issue. I'm curious if it is related to the fact that the label view is within a UITableViewCell subclass, but I don't have time to run a number of experiments. I typically haven't made much use of UILabels with attributed text, but I haven't found on here or elsewhere on the internet evidence of known issues.

I've looked through all the APIs to the best of my abilities, tried disabling certain word wrap settings, and tried setting the Plain label's font size to 14.0 before marking it as attributed because I thought it might be possible the higher-level attributes could be overriding the lower-level attributed text settings. Once again, this approach was not successful.

Has anyone else experienced anything like this?

In the meantime, I will proceed towards implementing my own attributed label as a UIView subclass. If this ends up being the only viable solution for the time being, I'll post the code.

5

5 Answers

1
votes

I don't know how you're setting the text in your label, but if you set it with an attributed string that has its font attribute set to what you want, it will work.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSString *plain = self.theData[indexPath.row];
    cell.label.attributedText = [[NSAttributedString alloc] initWithString:plain attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Snell Roundhand" size:24]}];
    return cell;
}

I saw the same problems you did when I tried to setup the label in the storyboard to use attributed text.

0
votes

As discussed, here is the solution I ended up using. It is not as versatile as UILabel in all respects (doesn't support scaling down of fonts etc.) but it turned out to be suitable for my needs.

@interface DHAttributedLabel : UIView
@property (nonatomic, strong) NSAttributedString * attributedText;
@end

@implementation DHAttributedLabel

-(id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

-(void) drawRect:(CGRect)rect {
    CGRect boundingRect = [_attributedText
        boundingRectWithSize    : CGSizeMake(rect.size.width, CGFLOAT_MAX)
        options         : NSStringDrawingUsesLineFragmentOrigin
        context         : nil
    ];
    CGRect drawInRect = rect;
    CGFloat dy = .5 * (rect.size.height - boundingRect.size.height);
    if (dy > 0) { // center vertically if appropriate
        drawInRect.origin.y += dy; // (truncate bottom if the text is too big)
    }
    [_attributedText drawInRect:drawInRect];
}

@end
0
votes

I have resolved the same issue by setting attributedText in the UITableViewDataSource cellForRowAtIndexPath: method. Seems that if attributedText property is set via IBOutlet before cell is returned by delegate, some of the attributedText properties are lost (like font size for attributed substring) while others are preserved (like text color for attributed substring).

Seems to me some kind of UIKit issue.

0
votes

In my case it worked when setting attributedText in tableView(_:willDisplay:forRowAt:).

0
votes

Attributed Text having HTML content, Means they having their own font specified in their content.

For increasing size in Label attributed Text you need to override the html Font via CSS.

Here is the Content which you get I.e HTMl Content.

<html><body><font face='Helvetica' size='3'>There is no description.</html></body>

As you can see, they have given size in HTML content You need to add extra property to override that font like this.

<style>
html * {font-size: 25.0pt !important;}
</style>
<html><body><font face='Helvetica' size='13'>There is no description.</html></body>

By adding this CSS, try to get attributed String, Set in the Label.

Hope this will increase the font size to 25.0.