Tag: nstextattachment

使用NSTextAttachment设置NSAttributedString的截尾的垂直alignment

我使用下面的代码在iOS 8中为UILabel生成一个NSAttributedString 。 // a long long Chinese title NSString *title = @"这是一个很长很长很长很长很长很长的中文标题"; // setup icon attachment NSTextAttachment *iconAttachment = [[NSTextAttachment alloc] init]; iconAttachment.image = [UIImage imageNamed:imageName]; iconAttachment.bounds = bounds; NSAttributedString *ycardImageString = [NSAttributedString attributedStringWithAttachment:iconAttachment]; // setup attributed text NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title]; if (shouldShowYcard) { [attributedText insertAttributedString:ycardImageString atIndex:0]; [attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" […]

如何在NSTextAttachment中设置模板图像的颜色

如何设置作为属性string附件的模板化图像的颜色? 背景: 我有一个UILabel,我将其设置为一个NSAttributedString。 NSAttributedString包含一个带有小图片的NSTextAttachment。 现在我想让我的图像颜色匹配文本颜色,我无法弄清楚如何使其工作。 我通常希望通过将其渲染模式设置为UIImageRenderingModeAlwaysTemplate,然后在包含UIView上设置tintColor来着色图像。 我试过在我的UILabel上设置tintColor,但是没有效果。 这是我的代码。 它在Ruby(RubyMotion)中,所以语法可能看起来有点有趣,但是它与ObjectiveC 1:1映射。 attachment = NSTextAttachment.alloc.initWithData(nil, ofType: nil) attachment.image = UIImage.imageNamed(icon_name).imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate) label_string = NSMutableAttributedString.attributedStringWithAttachment(attachment) label_string.appendAttributedString(NSMutableAttributedString.alloc.initWithString('my text', attributes: { NSFontAttributeName => UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote), NSForegroundColorAttributeName => foreground_color })) label = UILabel.alloc.initWithFrame(CGRectZero) label.tintColor = foreground_color label.attributedText = label_string label.textAlignment = NSTextAlignmentCenter label.numberOfLines = 0

当使用图像和文本时,NSAttributeString高度似乎alignment错误

我在tableviewcell中有一个uilabel,标签使用NSAttributeString来支持图像和文本。 但是我发现,当标签仅包含图片或文字时,标签的高度是正确的,但是当标签中包含图片和文字时,高度似乎不正确,行高看起来比实际高度需要量稍大 如果只有文字或图片,高度是正确的 我使用NSTextAttachment的自己的子类来构build图像 和附件代码是这样的

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

我收到一个NSAttributedString作为input,它可能包含一个作为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: "")) } } 如果textView.attributedText包含多个附件(我在其string看到了几个\u{ef} ),我希望枚举匹配条件if (value as? NSTextAttachment) != nil几次,但是该代码块是只执行一次。 我如何删除所有附件?

在一个UILabel中,是否有可能强制一行不在某个地方中断

我有一个应该是两行的UILabel。 文本被本地化为法文和英文。 我正在设置标签的属性文字属性。 文本由三个附加string构成,如textFragment1,textFragment2和textFragment3。 中间string实际上是用NSTextAttachment创build的图像。 我想确保textFragment2和textFragment3在同一行。 第一个string可能会或可能不会换行取决于它的长度。 问题是我的法文文本目前很短,所以行在textFragment2之后。 我通过在textFragment1的本地化法语文本中添加一个换行符号来暂时解决了这个问题。 虽然我不太喜欢这个解决scheme。 我想知道是否有办法处理textFragment2和textFragment3,以便他们将永远在一起在同一行。

麻烦保存NSAttributedString,与图像,到一个RTF文件

我有一些输出是一个非常简单的RTF文件。 当我生成这个文件,用户可以通过电子邮件发送。 所有这一切正常。 文件看起来不错。 一旦我有NSAttributedString,我做了一个NSData块,并将其写入文件,如下所示: NSData* rtfData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType} error:&error]; 这个文件可以通过电子邮件发送。 当我检查电子邮件都很好。 现在,我负责在文档的顶部添加一个UIImage。 太棒了,所以我创build了一个属性如下所示的string: NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; UIImage* image = [UIImage imageNamed:@"logo"]; attachment.image = image; attachment.bounds = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); NSMutableAttributedString *imageAttrString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy]; // sets the paragraph styling of the text attachment NSMutableParagraphStyle […]

更改NSTextAttachment图像的大小?

我正在使用下面的代码来格式化RSS源的原始HTML到NSAttributedString 。 HTML包含<img>标签,这些标签NSTextAttachment图像作为NSTextAttachment附加到NSAttributedString 。 问题是,该图像是太大的UITextView属性string放在中。有没有一种方法,我可以在属性的string中操纵图像的大小? 下面是代表图像附件的整个属性string的摘录。 { 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 […]

如何调整图像大小或完成NSAttributedString NSTextAttachment(或设置其初始大小)

我有一个NSAttributedString我添加一个NSTextAttachment。 图像是50瓦50小时,但我想它缩小以反映属性string的行高度。 我以为这会自动完成,但我猜不是。 我已经看了UImage类的引用,但这个图像似乎并没有在UIImageView中设置,所以没有访问框架属性。 以下是我目前拥有的截图: 在一个理想的世界里,我还想实现一种基于用户input(例如增加字体大小)来放大图像的方式。 任何想法如何实现这一目标? 谢谢 编辑1 这是我如何创build它: NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = [UIImage imageNamed:@"note-small.png"]; NSLog(@"here is the scale: %f", textAttachment.image.scale); NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withString:@" "]; [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withAttributedString:attrStringWithImage];