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.
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.
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 underNSFontAttributeName
was immutable.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.