标签: nstextattachment

如何检测 NSAttributedString 是否包含 NSTextAttachment 并将其删除?

我收到作为输入的 a NSAttributedString,其中可能包含附加为 的图像NSTextAttachment。我需要检查是否真的附加了这样的图像,在这种情况下,将其删除。我一直在寻找相关的帖子但没有成功,我该怎么做?

编辑:我正在尝试这个:

let mutableAttrStr = NSMutableAttributedString(attributedString: textView.attributedText)
textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, textView.attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in

            if (value as? NSTextAttachment) != nil {
                mutableAttrStr.replaceCharacters(in: range, with: NSAttributedString(string: ""))
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果textView.attributedText包含多个附件(我\u{ef}在它的 中看到多个附件string),我希望枚举if (value as? NSTextAttachment) != nil多次匹配条件,但该代码块只执行一次。

如何删除所有附件?

nsattributedstring nstextattachment ios swift

4
推荐指数
1
解决办法
4985
查看次数

iOS 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];
    lblHeadline.attributedText = lockString;
    [lblHeadline sizeToFit];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当边缘附近的文本时,锁图标丢失了.

当边缘附近的文本时,锁图标丢失了.

nsattributedstring nstextattachment ios

2
推荐指数
1
解决办法
4615
查看次数

为UITableViewCell加载NSTextAttachment的图像异步

在异步线程或图像缓存库(如SDwebimage)中动态加载图像.下面的代码是我尝试过的,它不会在从网络获取图像后重新绘制.

    let mutableAttributedString = NSMutableAttributedString()

    if let _img = newsItem.img {
        var attachment = NSTextAttachment()
        attachment.bounds = CGRectMake(4, 4, expectedWidth, expectedWidth * _img.ratio)

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            attachment.image = UIImage(data: NSData(contentsOfURL: NSURL(string: _img.src)!)!)
        })

        mutableAttributedString.appendAttributedString(NSAttributedString(attachment: attachment))
    }
Run Code Online (Sandbox Code Playgroud)

uitableview nstextattachment ios

2
推荐指数
1
解决办法
1979
查看次数

更改NSTextAttachment图像的大小?

我使用以下代码将RSS提要的原始HTML格式化为NSAttributedString.HTML包含的<img>标签可以将图像作为附件附加NSTextAttachmentNSAttributedString.问题是图像对于放置属性字符串的UITextView来说太大了.有没有办法可以在属性字符串中操纵图像的大小?以下是代表图像附件的整个属性字符串的摘录.

{
    NSAttachment = "<NSTextAttachment: 0x16d29450> \"e0e1d483a922419807a2378a7ec031af.jpg\"";
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICTFont: 0x16ef8a40> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace …
Run Code Online (Sandbox Code Playgroud)

objective-c nstextattachment ios

2
推荐指数
1
解决办法
3661
查看次数

带有前景色的属性文本中的 NSTextAttachment 图像

当我将图像附件添加到带有前景色设置的 UITextView 时,图像会被设置的颜色消隐:

在此处输入图片说明

let attrString = NSMutableAttributedString(string: rawText, attributes: [.font: UIFont.systemFont(ofSize: 17), .foregroundColor: UIColor.black])
let attachment = NSTextAttachment(image: image)
let imgStr = NSMutableAttributedString(attachment: attachment)
attrString.append(imgStr)
textview.attributedText = attrString
Run Code Online (Sandbox Code Playgroud)

当我删除 时.foregroundColor: UIColor.black,图像显示正确,但我需要能够设置属性文本颜色。

我试图.foregroundColor在添加图像附件后明确删除该属性,但没有运气。我还尝试.foregroundColor从大部分文本中删除该属性,但它仍然不起作用,只能从整个字符串中删除该属性:

attrString.removeAttribute(.foregroundColor, range: NSRange(location: attrString.length-1, length: 1)) // does not work
// -------
attrString.removeAttribute(.foregroundColor, range: NSRange(location: 1, length: attrString.length-1)) // does not work
// -------
attrString.removeAttribute(.foregroundColor, range: NSRange(location: 0, length: attrString.length)) // works but no text colour
Run Code Online (Sandbox Code Playgroud)

这是在 Xcode 11.0、iOS 13 上开发的。这是 UITextView/iOS …

nsattributedstring nstextattachment swift

2
推荐指数
1
解决办法
993
查看次数