我想将NSTextAttachment图像附加到我的属性字符串并使其垂直居中.
我使用以下代码来创建我的字符串
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:DDLocalizedString(@"title.upcomingHotspots") attributes:attrs];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [[UIImage imageNamed:@"help.png"] imageScaledToFitSize:CGSizeMake(14.f, 14.f)];
cell.textLabel.attributedText = [str copy];
Run Code Online (Sandbox Code Playgroud)
但是,图像看起来与单元格textLabel的顶部对齐.

如何更改绘制附件的矩形?
如何设置作为属性字符串附件的模板化图像的颜色?
背景:
我有一个UILabel,我将其attributionText设置为NSAttributedString.NSAttributedString包含带有小图像的NSTextAttachment.现在我想让我的图像颜色与文本颜色匹配,我无法弄清楚如何使它工作.
我通常希望通过将其渲染模式设置为UIImageRenderingModeAlwaysTemplate然后在包含UIView上设置tintColor来为图像着色.我试过在我的UILabel上设置tintColor但是没有效果.
这是我的代码.它在Ruby(RubyMotion)中,因此语法可能看起来有点滑稽,但它与Objective C 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
Run Code Online (Sandbox Code Playgroud) 我有一个NSAttributedString我要添加一个NSTextAttachment.图像是50w乘50h,但我希望它缩小以反映属性字符串的行高.我以为这会自动完成,但我猜不会.我查看了UImage类引用,但是这个图像似乎没有在UIImageView中设置,因此无法访问框架属性.这是我目前拥有的截图:

在理想的世界中,我还想实现一种基于用户输入(例如增加字体大小)来放大图像的方法.关于如何实现这一点的任何想法?
谢谢
这是我如何创建它:
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];
Run Code Online (Sandbox Code Playgroud) 我可以NSAttributedString使用转义的换行符(@"\n")创建一个多行.使用iOS 7,我现在可以嵌入一个UIImage内部属性字符串(via NSTextAttachment).
我注意到每当我将attributedTexta 设置为UILabel带有嵌入图像的多行属性字符串时,实际显示的行数与标签的高度成反比.例如,当标签的高度为80时,会出现两条线; 当高度大约为100时,只出现第二条线; 当高度约为130时,什么都没有出现.
尝试将多个UILabel并排放置在a UITableViewCell内并使标签与单元格高度(垂直)增长时发生此问题.
任何人都可以解释为什么会这样吗?有没有人知道不涉及使UILabel变小的解决方法?
示例代码:
@implementation SOViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc] init];
[text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 1\n"]];
[text1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"Line 2"]];
UIImage *image = [UIImage imageNamed:@"17x10"]; //some PNG image (17px by 10px)
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
NSMutableAttributedString *text2 = [[NSMutableAttributedString …Run Code Online (Sandbox Code Playgroud) 检测用户何时点击NSTextAttachmentiOS 的最佳方法是什么?
我认为,其中一种方法是检查Carret的位置是否有NSAttachmentCharacter,但它似乎并不正确.
我也尝试过UITextViewDelegate方法:-(BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange但是当它没有被调用时textView.editable=YES
我有一个UILabel应该是两行长.该文本已本地化为法语和英语.
我正在设置标签的attributedText属性.该文本由三个附加的字符串构成,例如textFragment1,textFragment2和textFragment3.中间字符串实际上是使用NSTextAttachment创建的图像.
我想确保textFragment2和textFragment3在同一行.第一个字符串可能会或可能不会换行,具体取决于它的长度.问题是我的法语文本目前相当短,所以该行在textFragment2之后换行.
我通过在textFragment1的本地化法语文本中添加换行符来暂时修复此问题.我不喜欢这个解决方案.我想知道是否有办法处理textFragment2和textFragment3,以便它们将始终在同一行上.
这是我的问题:
我使用Core Data来存储来自iOS和/或OS X应用程序的富文本输入,并希望将图像粘贴到NSTextView或UITextView中,以便:a)保留其原始分辨率,b)在显示屏上进行缩放以正确调整textView,这意味着根据设备上视图的大小进行缩放.
目前我正在使用- (void)textStorage:(NSTextStorage *)textStorage didProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger)delta查找附件,然后生成具有比例因子的图像并将其分配给textAttachment.image属性.
这样做是因为我只是改变了比例因子并保留了原始图像,但我相信更优雅的解决方案是使用NSTextAttachmentContainer子类并从中返回一个适当大小的CGREct
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何创建和插入这样的子类?
我是否使用textStorage:didProcessEditing迭代每个附件并将NSTextAttachmentContainer替换为我自己的类,或者我可以简单地创建一个Category,然后以某种方式使用此类别来更改默认行为.后者似乎不那么具有侵入性,但如何让我的textViews自动使用这个类别呢?
糟糕:刚刚注意到NSTextAttachmentContainer是一个协议,所以我假设然后在NSTextAttachment上创建一个类别,并覆盖上面的方法是一个选项.
嗯:不能使用Category来覆盖现有的类方法,所以我想子类化是唯一的选择,在这种情况下如何让UITextView使用我的附件子类,或者我必须迭代使用referencedString来替换所有NSTextAttachments MYTextAttachment的实例.将此字符串取消归档到OS X上会产生什么影响,比如默认的OS X NSTextAttachment(与iOS类不同)?
我按照以下文章介绍了如何使用NSTextAttachment添加与UILabel内联的图像.我尽我所能,并在Swift中编写了我的版本.
我正在创建一个聊天应用程序,我正在插入啤酒图标的字段不会渲染图像或似乎没有内嵌渲染图像.我没有收到任何错误,所以我假设我在代码中遗漏了一些小细节.
var beerName:String!
if(sender == bn_beer1)
{
beerName = "beer1.png"
}
if(sender == bn_beer2)
{
beerName = "beer2.png"
}
if(sender == bn_beer3)
{
beerName = "beer3"
}
var attachment:NSTextAttachment = NSTextAttachment()
attachment.image = UIImage(named: beerName)
var attachmentString:NSAttributedString = NSAttributedString(attachment: attachment)
var myString:NSMutableAttributedString = NSMutableAttributedString(string: inputField.text)
myString.appendAttributedString(attachmentString)
inputField.attributedText = myString;
Run Code Online (Sandbox Code Playgroud) 我有一个UILabel显示NSAttributedString.该字符串包含文本和UIImagea NSTextAttachment.
在呈现时,有没有一种方式来获得的位置NSTextAttachment的UILabel?
编辑
这是我想要达到的最终结果.
当文本只有1行长时,图像应该在正确的边缘UILabel.简单:

如果您有多行,但仍希望图像位于最后一行的末尾,则会出现问题:

我有一个NSTextAttachment,我想显示一个加载图像,直到图像下载,然后一旦它我想要更新图像.
我有所有的逻辑,除非我textAttachment.image = image第二次打电话没有任何反应.
如何更新NSTextAttachment已经由UITextView?呈现的?
谢谢!
nstextattachment ×10
ios ×7
ios7 ×4
uilabel ×4
objective-c ×3
swift ×2
uiimage ×2
ios8 ×1
textkit ×1
uitextview ×1