简体   繁体   中英

NSTextAttachment not showing

I am unable to get the NSTextAttachment to show for the NSAttributedString returned in the function below.

- (NSAttributedString *) tokenString:(NSUInteger) tokens
{
CGFloat size = 20;
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.bounds = CGRectMake(0,0,size,size)
attachment.image = [[UIImage imageNamed:@"ContestTokenSmall"] imageScaledToSize:CGSizeMake(size, size)];

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %ld", (long)tokens] attributes:[self attributedStringAttributes]];
[str insertAttributedString:attachmentString atIndex:0];
return str;
}

The label I'm using is as follows:

- (UILabel *) tokenLabel
{
if (_tokenLabel == nil)
{
    _tokenLabel = [[UILabel alloc] init];
    _tokenLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _tokenLabel.font = [UIFont zbd_boldFontWithSize:13.0];
    _tokenLabel.textColor = UIColor.whiteColor;
    [_tokenLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [_tokenLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_tokenLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [_tokenLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return _tokenLabel;
}

I've tried removing the NSTextAttachment bounds, changing/removing image resizing, and using a different image but nothing I do seems to make an image appear in the label.

Is there something wrong in the code above? Is there a property I need to set in the label to make the image show?

Thanks in advance!

EDIT

How I set the label:

self.tokenLabel.attributedText = [self tokenString:award.topBid];

Log of string:

2018-04-24 16:03:14.579429-0500 TestGame1[44243:1597097] {
    NSAttachment = "<NSTextAttachment: 0x6040002c2e60>";
} 10{
    NSColor = "UIExtendedGrayColorSpace 1 1";
    NSFont = "<UICTFont: 0x7ff0df85ff10> font-family: \"Domus\"; font-weight: bold; font-style: normal; font-size: 13.00pt";
}

Also, I see an empty space where the image should be.

UPDATE 2

The image created with the code below will show in the attributed string, but still none of the pdf or png images loaded from Media.xcassets will show.

CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor.redColor CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

I figured it out. The problem was with [UIImage imageNamed:@"ContestTokenSmall"] . Since the image was located in the framework the this code is in, I needed to use

+ (nullable UIImage *)imageNamed:(NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection NS_AVAILABLE_IOS(8_0);

so that it would get the image from the correct bundle. I was getting the image from the framework's bundle while debugging and that is why the image would show in image views while not showing in the attributed string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM