归因于UITextView

时间:2016-08-05 19:33:03

标签: uitextview nsattributedstring ios10 ios9.3

我尝试将图片添加到文字视图文字

enter image description here

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    [self updateTextViewWithImage:image];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)updateTextViewWithImage:(UIImage *)image{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_textView.text];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = image;

    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(2, 1) withAttributedString:attrStringWithImage];
    _textView.attributedText = attributedString;
}

在xcode调试器中,我确实看到正确添加了文本attachemnt。

在方法调用之前:

   Hello{
    NSFont = "<UICTFont: 0x1004333f0> font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}

方法调用后:

He{
    NSFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}{
    NSAttachment = "<NSTextAttachment: 0x1702a2880>";
    NSFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}lo{
    NSFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

但是即使在调用该方法之后,文本视图也没有在其中显示图像。而且textView也不是nil。我能够将图像作为子视图添加到文本视图中,但我想使用归因字符串。

编辑:我还尝试将图片大小调整为100x100,并在故事板中设置属性文本。但仍然没有图像添加到文本中。

奇怪的是,如果创建textView并将其添加为子视图,则会将图像添加到属性文本中。

-(void)updateTextViewWithImage:(UIImage *)image{

    UIImage *img=[self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];

    UITextView *textView = [[UITextView alloc] initWithFrame:_textView.frame];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = img;

    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
    textView.attributedText = attributedString;
    _textView.attributedText=attributedString;
    [_textView addSubview:textView];

}

如果在IB中创建了textView,则不添加图像。

编辑:

我检查了文本视图地址,它在viewDidAppear和我得到图像后是相同的。

<UITextView: 0x7d085a00   - at viewWillAppear

<UITextView: 0x7d085a00; - at updateTextViewWithImage:

在此编辑中,我使用_textView

发短信
-(void)updateTextViewWithImage:(UIImage *)image{

        UIImage *img=[self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];

        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"];
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
        textAttachment.image = img;

        NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
        [attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
        _textView.attributedText=attributedString;

    }

在我调用updateTextViewWithImage后,我检查了viewDebugger:

enter image description here

并且未使用属性字符串更新textView。

1 个答案:

答案 0 :(得分:0)

我发现错误在于我的观点WillappearMethod。

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_textView becomeFirstResponder];

    //get the notes from datatabse
    if ([_notesStatus isEqualToString:@"UpdateNotes"]) {

        _textView.text=[_myManager getStringForRowWithId:_notesID];
        NSLog(@"text view addres %@",_textView);

    }
}

取消摄像头选择器后,将调用viewWillAppear,_textView.text将覆盖属性字符串。