1

I have a UILabel that I update dynamically. There are basically two axes of variation (single vs double line, and whether it represents a missing item). I cannot get the attributed single line (the first branch in the code below) to show up (though the NSLog shows what looks like a well formed NSAttributedString).

if (self.valve.name.length == 0) {
    if (self.valve.isMissing) {
        NSAttributedString *text = [[NSAttributedString alloc] initWithString: self.valve.serialID attributes: [self attributesForMissing]];
        self.nameAndIDLabel.attributedText = text;
        NSLog(@"missing single label = %@", text);
    }
    else {
        self.nameAndIDLabel.text = self.valve.serialID;
    }
}
else {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: [NSString stringWithFormat: @"%@\n    ", self.valve.name]];
    [text appendAttributedString: [[NSAttributedString alloc] initWithString: self.valve.serialID attributes: @{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}]];
    if (self.valve.isMissing) {
        [text
            addAttributes: [self attributesForMissing]
            range: NSMakeRange(0, text.length)];
        NSLog(@"missing multi label = %@", text);
    }
    self.nameAndIDLabel.attributedText = text;
}

The type of output that comes out from the NSLog for the missing one looks like:

missing single label = D002614{
    NSColor = "UIDeviceRGBColorSpace 1 0 0 1";
    NSObliqueness = "0.3";
}

The type that shows up for the one that does show up (same attributes added, just multi line) looks like:

missing multi label = Big Gun Tank SW
    {
    NSColor = "UIDeviceRGBColorSpace 1 0 0 1";
    NSObliqueness = "0.3";
}A005261{
    NSColor = "UIDeviceRGBColorSpace 1 0 0 1";
    NSFont = "<UICTFont: 0x166425f0> font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSObliqueness = "0.3";
}
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
  • 2
    Are you sure that `self.nameAndIDLabel` is not `nil`? Are you sure the text color isn't the same as the background? – rmaddy Jan 16 '14 at 22:19
  • @rmaddy it is not nil. As for the text color, you can see I'm using the same set of attributes in the main `else` branch. And those show up fine (red and oblique). I'll add an update showing the `NSLog` output. – Travis Griggs Jan 16 '14 at 22:42
  • Try setting a font for the single label. – rmaddy Jan 16 '14 at 23:57
  • possible duplicate of [iOS 7 BUG - NSAttributedString does not appear](http://stackoverflow.com/questions/19127828/ios-7-bug-nsattributedstring-does-not-appear) – matt Jan 17 '14 at 01:05
  • and see my answer there, http://stackoverflow.com/a/19409962/341994 – matt Jan 17 '14 at 01:06
  • I have discovered through testing that it is the obliqueness attribute. I was honestly just using that for a quick and dirty "italic" so I'll go figure out how to do italics legit. – Travis Griggs Jan 17 '14 at 22:12
  • If text is very large, then you need to use UITextView with scrolling enabled and editable/selected disabled (instead of UIScrollView & Content view & UILabel). – Borzh Aug 09 '18 at 16:51
  • Where is attributesForMissing defined? – David Rector Jul 24 '19 at 01:51

0 Answers0