我已经花了3个小时,但是无法解决它,有人知道为什么吗?请帮我!
下面的代码添加了一个图像作为属性字符串的附件,
UIImage *img = [UIImage imageNamed:imgName];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
[attrStr appendAttributedString:attrStringWithImage];
Run Code Online (Sandbox Code Playgroud)
然后我将attrStr分配给UITextView的attributionText,这在iOS 7中运行良好但在iOS 6中没有显示图像,我听说自iOS 6以来UITextView支持的属性文本,我应该为iOS 6做什么不同?
PS:在iOS 6中,如果我将attrStr分配给UILabel的attributedText,它会显示,UITextView的特殊之处是什么?
我有一个NSTextAttachment,我想显示一个加载图像,直到图像下载,然后一旦它我想要更新图像.
我有所有的逻辑,除非我textAttachment.image = image第二次打电话没有任何反应.
如何更新NSTextAttachment已经由UITextView?呈现的?
谢谢!
我正在关注这个很酷的教程@Duncan Groenewald 在OS X和iOS上使用图像实现富文本,并能够在我的显示中显示图像UITextView.但是,这些图像并没有按照我希望的方式居中.见图

如您所见,我希望我的图像以X轴为中心.
我尝试rect使用适当的值返回,-attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex但没有帮助.
我也尝试设置NSKernAttributeName的NSTextAttachment属性串.但它所做的只是隐藏图像.
我正试图在左边有一个带有图像和标题的UIlabel,在右边有一个带有项目符号的描述列表,为此我正在使用NSAttributedString,如下所示:
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.tabStops =
@[ [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:tabLocation options:[NSDictionary dictionary]] ];
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"test_image"];
textAttachment.bounds = CGRectMake(0, -3, 15, 15);//resize the image
attString = [NSAttributedString attributedStringWithAttachment:textAttachment].mutableCopy;
[attString appendAttributedString:[[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"title\t\u2022 %@",
[@[ @"description1", @"description2" ]
componentsJoinedByString:@"\n\t\u2022 "]]
attributes:@{NSParagraphStyleAttributeName : pStyle}]];
label.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)
我希望右边的列表保持对齐但事实并非如此,这是我得到的结果:
我正在使用以下代码在iOS 8中生成NSAttributedStringfor UILabel.
// 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:@" "] atIndex:1];
[attributedText addAttribute:NSBaselineOffsetAttributeName value:@(offset) range:NSMakeRange(0, 1)];
}
NSRange titleRange = NSMakeRange(shouldShowYcard ? 2 : 0, title.length);
[attributedText addAttribute:NSFontAttributeName value:font range:titleRange];
[attributedText …Run Code Online (Sandbox Code Playgroud) 我正在使用ios7中的Text Kit构建一个注释编辑器.早些时候,我遇到了麻烦,在自定义尺寸的渲染NSTextAttachment的AS它呈现放缓的大extent.I解决这个问题通过缩放图像,然后将它们添加到textview.You可以找到我的回答 的iOS 7.0的UITextView gettings后非常慢向图像添加图像 缩放图像后,文本视图渲染运行正常,没有任何滞后.文本视图的属性文本存储在核心数据中.在应用程序运行时,textview正确显示图像.即使在核心中保存属性文本后也是如此数据并再次检索以显示在textview上,图像看起来很好.但是在杀死应用程序并再次运行应用程序之后.图像被放大到2倍大小.在缩放图像时我使用了以下功能并使用了[[UIScreen bounds]比例]以保持图像质量.
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Run Code Online (Sandbox Code Playgroud)
如果我将图像缩放到1.0,图像不会扩展,但图像质量非常差.
我认为问题出在哪里? 问题在于布局管理器.
我曾尝试
我试图子类NSLayoutManager并重写
- (空白)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)原产
我看到的是运行application.If我尝试的一个新的会话时,附件大小加倍检查附件的大小并调整大小.滞后开始再次出现.我很快就遇到了这个问题.任何建议都会非常感激.

我有一个被覆盖的NSTextAttachment子类
attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:
imageForBounds:textContainer:characterIndex:.
我需要在某些时候重新绘制附件.调用setNeedsDisplayUITextView不起作用.
有任何想法吗?我想避免重新创建附件和/或属性字符串.
我在NSTextView [1]中编辑HTML的子集,我想模拟<hr>标签.
我已经想通了,顺便做它与NSTextAttachment和一个自定义NSTextAttachmentCell,并有所有的代码写入到插入附件和电池.问题是,单元格下面有大量的空白区域.
这个空间是不是电池的部件本身,如果我画了电池的整个面积的红色,这是完全正确的大小,但文中观点是把文本的下一行很远红下方.数量似乎取决于单元格上方的文本数量; 不幸的是,我正在使用<hr>标签至关重要的长文档,这会导致应用程序出现重大问题.
到底他妈发生了什么?
我的细胞亚类的钱部分:
- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer
proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position
characterIndex:(NSUInteger)charIndex {
lineFrag.size.width = textContainer.containerSize.width;
lineFrag.size.height = topMargin + TsStyleBaseFontSize *
heightFontSizeMultiplier + bottomMargin;
return lineFrag;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
characterIndex:(NSUInteger)charIndex
layoutManager:(NSLayoutManager *)layoutManager {
NSRect frame = cellFrame;
frame.size.height -= bottomMargin;
frame.size.height -= topMargin;
frame.origin.y += topMargin;
frame.size.width *= widthPercentage;
frame.origin.x += (cellFrame.size.width - frame.size.width)/2;
[color set];
NSRectFill(frame);
}
Run Code Online (Sandbox Code Playgroud)
[1]我试图与isEditable集的web视图和它产生的标记是unusably脏尤其,我无法找到一种方法来包装<p>标签文本很好.
要回答Rob Keniger对插入水平规则附件的代码的请求:
- (void)insertHorizontalRule:(id)sender {
NSAttributedString * rule = [TsPage newHorizontalRuleAttributedStringWithStylebook:self.book.stylebook];
NSUInteger loc = self.textView.rangeForUserTextChange.location; …Run Code Online (Sandbox Code Playgroud) 我在UITextView上尝试了它的工作原理.但是当我使用CATextLayer时,图像留空了.
有人试过吗?
这是我在UIView drawRect中尝试过的代码
NSTextAttachment* attachment = [NSTextAttachment new];
attachment.image = [UIImage imageNamed:@"rainbow.png"];
NSAttributedString* imageString = [NSAttributedString attributedStringWithAttachment:attachment];
NSDictionary * attribute = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12]};
NSMutableAttributedString *speechString = [[NSMutableAttributedString alloc] initWithString: @"hi" attributes:attribute];
[speechString insertAttributedString:imageString atIndex:1];
[speechString appendAttributedString:imageString];
Run Code Online (Sandbox Code Playgroud)
然后添加到CATextLayer
CATextLayer * speechTextLayer = [CATextLayer layer];
[speechTextLayer setString:speechString];
[speechTextLayer setFrame:self.bounds];
[speechTextLayer setBackgroundColor:[UIColor whiteColor].CGColor];
[self.layer addSublayer:speechTextLayer];
Run Code Online (Sandbox Code Playgroud) nstextattachment ×10
ios ×5
uitextview ×5
objective-c ×2
alignment ×1
calayer ×1
cocoa ×1
core-text ×1
image ×1
ios7 ×1
nstexttab ×1
nstextview ×1
sdk ×1
swift3 ×1
textkit ×1
uiimage ×1
uilabel ×1