Подтвердить что ты не робот

Добавление изображений в UITextView

В моем приложении у меня есть UITextView и кнопка чуть ниже текстового вида, чтобы вставить фотографию во UITextView во время редактирования.

Мое требование состоит в том, что пользователь может редактировать текст внутри и вставлять изображения при необходимости.

Как и у приложения StackOverflow собственный UITextView:

enter image description here

4b9b3361

Ответ 1

Вы можете добавить представление изображения в виде subView UITextView.

Создайте изображение с изображением:

UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[imageView setFrame:yourFrame];
[yourTextView addSubview:imageView];

Edit:

Чтобы избежать использования перекрытия (Спасибо @chris):

CGRect aRect = CGRectMake(156, 8, 16, 16);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame), CGRectGetMinY(imageView.frame), CGRectGetWidth(yourTextView.frame), CGRectGetHeight(imageView.frame))];
yourTextView.textContainer.exclusionPaths = @[exclusionPath];
[yourTextView addSubview:imageView]; 

Ответ 2

Если вы добавите его только как подвью, некоторый текст может быть "позади" изображения. Поэтому добавьте код, который "скажет" текст, эта область изображения недоступна:

UIBezierPath *exclusionPath = [UIBezierPath    bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),    
CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame), 
CGRectGetHeight(imageView.frame))];

textView.textContainer.exclusionPaths = @[exclusionPath];

Ответ 3

Отметьте это, ios-5-rich-text-editing-series. В iOS 5 вы можете вставлять изображения и использовать HTML-тексты. Возможно, вам придется использовать UIWebview и webkit.

Вы также можете проверить EGOTextView, который имеет множество функций редактирования текста.

Ответ 4

просто добавьте в качестве подзапроса TextView, как показано ниже.

    [yourTextView addSubview:yourImageView];

Ответ 5

Создайте подкласс UITextView и переопределите этот метод

- (void)paste:(id)sender 
{
    NSData *data = [[UIPasteboard generalPasteboard] dataForPasteboardType:@"public.png"];

    if (data) 
    {

        NSMutableAttributedString *attributedString = [[self attributedText] mutableCopy];

        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];

        textAttachment.image = [UIImage imageWithData:data scale:5];

        NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];

        [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:attrStringWithImage];

        self.attributedText = attributedString;

    }
    else
    {

        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

        NSAttributedString *text = [[NSAttributedString alloc] initWithString:pasteBoard.string];

        NSMutableAttributedString *attributedString = [self.attributedText mutableCopy];

        [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:text];

        self.attributedText = attributedString;

    }
}