首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextAttachment未显示

NSTextAttachment未显示
EN

Stack Overflow用户
提问于 2018-04-24 19:48:28
回答 1查看 512关注 0票数 1

我无法让NSTextAttachment显示在下面函数中返回的NSAttributedString

代码语言:javascript
复制
- (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;
}

我使用的标签如下:

代码语言:javascript
复制
- (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;
}

我尝试过删除NSTextAttachment边界,更改/删除图像大小,并使用不同的图像,但我所做的任何事情似乎都不会使图像出现在标签中。

上面的代码有什么问题吗?是否需要在标签中设置使图像显示的属性?

提前感谢!

编辑

我如何设置标签:

代码语言:javascript
复制
self.tokenLabel.attributedText = [self tokenString:award.topBid];

字符串日志:

代码语言:javascript
复制
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";
}

另外,我看到了一个空空间,图像应该在那里。

更新2

用下面的代码创建的图像将显示在属性化字符串中,但是从Media.xcassets加载的pdf或png图像仍然不会显示。

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-25 18:55:47

我想通了。问题在于[UIImage imageNamed:@"ContestTokenSmall"]。由于映像位于此代码所在的框架中,所以我需要使用

代码语言:javascript
复制
+ (nullable UIImage *)imageNamed:(NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection NS_AVAILABLE_IOS(8_0);

这样它就能从正确的包中得到图像。调试时,我从框架的包中获取图像,这就是为什么图像将显示在图像视图中,而不是显示在属性字符串中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50009726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档