标签: nstextattachment

在iOS 6中将附件图像添加到属性字符串

我已经花了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的特殊之处是什么?

nsattributedstring nstextattachment

9
推荐指数
1
解决办法
9924
查看次数

一旦渲染,就更新NSTextAttachment的图像

我有一个NSTextAttachment,我想显示一个加载图像,直到图像下载,然后一旦它我想要更新图像.

我有所有的逻辑,除非我textAttachment.image = image第二次打电话没有任何反应.

如何更新NSTextAttachment已经由UITextView?呈现的?

谢谢!

uitextview uiimage nstextattachment

9
推荐指数
1
解决办法
286
查看次数

NSTextAttachment图像对齐

我正在关注这个很酷的教程@Duncan Groenewald 在OS X和iOS上使用图像实现富文本,并能够在我的显示中显示图像UITextView.但是,这些图像并没有按照我希望的方式居中.见图

NSTextAttachment样本图像

如您所见,我希望我的图像以X轴为中心.

我尝试rect使用适当的值返回,-attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex但没有帮助.

我也尝试设置NSKernAttributeNameNSTextAttachment属性串.但它所做的只是隐藏图像.

sdk image uitextview nstextattachment ios

8
推荐指数
2
解决办法
4743
查看次数

NSAttributedString带有图像附件和NSTextTab,文本未对齐

我正试图在左边有一个带有图像和标题的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)

我希望右边的列表保持对齐但事实并非如此,这是我得到的结果:

在此输入图像描述

我期望列表是这样对齐的: 在此输入图像描述

nstextattachment core-text ios nstexttab nsparagraphstyle

8
推荐指数
1
解决办法
2037
查看次数

使用NSTextAttachment为NSAttributedString设置截断尾部的垂直对齐

我正在使用以下代码在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)

alignment nsattributedstring uilabel nstextattachment ios

8
推荐指数
1
解决办法
673
查看次数

iOS 7 UITextView:重新打开应用程序后,nstextattachment的大小增加2倍

我正在使用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我尝试的一个新的会话时,附件大小加倍检查附件的大小并调整大小.滞后开始再次出现.我很快就遇到了这个问题.任何建议都会非常感激.在此输入图像描述 在此输入图像描述

nslayoutmanager uitextview nstextattachment ios7 textkit

7
推荐指数
1
解决办法
3371
查看次数

使用属性文本在UITextView中重绘NSTextAttachments

我有一个被覆盖的NSTextAttachment子类
attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:
imageForBounds:textContainer:characterIndex:.

我需要在某些时候重新绘制附件.调用setNeedsDisplayUITextView不起作用.

有任何想法吗?我想避免重新创建附件和/或属性字符串.

uitextview nstextattachment ios

7
推荐指数
1
解决办法
2227
查看次数

NSTextAttachment周围的间距

如何在NSTextAttachments周围设置间距,如下例所示?

在示例中,无间距是我将NSTextAttachment附加到NSAttributedString时的默认行为.

在此输入图像描述

uitextview nsattributedstring nstextattachment swift3

7
推荐指数
3
解决办法
832
查看次数

NSTextAttachmentCell高一英里

我在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)

cocoa objective-c nstextview nstextattachment

6
推荐指数
1
解决办法
1433
查看次数

NSTextAttachment不会显示在CATextLayer上

我在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)

objective-c calayer nstextattachment ios

6
推荐指数
0
解决办法
270
查看次数