iOS NSTextAttachment图像未显示

iOS NSTextAttachment图像未显示,ios,nsattributedstring,nstextattachment,Ios,Nsattributedstring,Nstextattachment,NSTextAttachment锁定图像在边缘处被切断,但当线条在边缘处未断开时,可以看到锁定图标。我希望图标移动到下一行,就像单词移动到下一行一样 以下是示例: NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"lock.png"]; NSString *stringHeadline = @"This is a

NSTextAttachment锁定图像在边缘处被切断,但当线条在边缘处未断开时,可以看到锁定图标。我希望图标移动到下一行,就像单词移动到下一行一样

以下是示例:

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"lock.png"];
    NSString *stringHeadline = @"This is a example sample sentence. Why I do";
    NSAttributedString *attachmentLock = [NSAttributedString attributedStringWithAttachment:attachment];
    NSAttributedString *myText = [[NSMutableAttributedString alloc] initWithString:stringHeadline];
    NSMutableAttributedString *lockString = [[NSMutableAttributedString alloc] initWithAttributedString:myText];

    [lockString appendAttributedString:attachmentLock];
    lblHeadline.attributedText = lockString;
    [lblHeadline sizeToFit];


当文本靠近边缘时,锁定图标丢失。

只需在NSTextAttachment后添加空格即可。否则,当空间不足时,NSTextAttachment不会像普通文本那样更改为新行。我相信这是苹果的一个错误

您的代码应该如下所示:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"lock.png"];
NSString *stringHeadline = @"This is a example sample sentence. Why I do";
NSAttributedString *attachmentLock = [NSAttributedString attributedStringWithAttachment:attachment];
NSAttributedString *myText = [[NSMutableAttributedString alloc] initWithString:stringHeadline];
NSMutableAttributedString *lockString = [[NSMutableAttributedString alloc] initWithAttributedString:myText];

[lockString appendAttributedString:attachmentLock];
/** 
 * Here I'm adding a space after NSTextAttachment just to make sure that
 * the NSTextAttachment will auto change to next line like normal text does.
 * Otherwise the NSTextAttachment does not move to the new line.
 */
[lockString appendAttributedString: [[NSAttributedString alloc] initWithString:@" "]];
lblHeadline.attributedText = lockString;
[lblHeadline sizeToFit];

查看我的帖子,了解更多有关我的解决方案的信息。

在顶部图像中,文本也一直到最后,但它显示图像。@iDev图像不显示。但如果您看到文本,则两个文本都是same@iDev课文不一样。我添加了更多的单词,这样文本就不会停在边缘,这样图像就会显示出来。删除
sizeToFit()
并更改高度我有一个类似的错误,如果文本附件靠近边缘,它就会消失。添加空间触发了一个新行。谢谢,我也遇到了同样的问题,您的解决方案可能为我节省了几个小时!