属性文本在 UITextView 中不显示文本附件
Attributed text not showing text attachments in UITextView
我正在尝试将图像添加到文本视图文本
-(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 调试器中,我确实看到正确添加了文本附件。
方法调用前:
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";
}
但即使在调用该方法后,文本视图也不会在 it.Also 中显示图像想使用属性字符串。
编辑:我还尝试将图像大小调整为 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:
并且 textView 未使用属性字符串更新。
我发现错误出在我的viewWillappearMethod上。
-(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 将覆盖属性字符串。
我正在尝试将图像添加到文本视图文本
-(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 调试器中,我确实看到正确添加了文本附件。
方法调用前:
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";
}
但即使在调用该方法后,文本视图也不会在 it.Also 中显示图像想使用属性字符串。
编辑:我还尝试将图像大小调整为 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:
并且 textView 未使用属性字符串更新。
我发现错误出在我的viewWillappearMethod上。
-(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 将覆盖属性字符串。