Как я могу центрировать изображение на textView, который прикреплен к типу NSTextAttachment?

У меня есть textView (программно), и я вставил изображение, добавленное к NSTextAttachment, и хочу, чтобы оно было выровнено по центру текстового представления... но я еще не нашел никаких решений... Мне было интересно, если эта задача можно только через код...

    let image = ResizeImage(image: UIImage(named: "logo")!, targetSize: size)

    let image1Attachment = NSTextAttachment()

    image1Attachment.image = image

    let image1Attachments = NSAttributedString(attachment: image1Attachment)

Моим лучшим подходом к решению при попытке добиться этого был этот код:

    let image = ResizeImage(image: UIImage(named: "telepaint1")!, 
    targetSize: size)

//let textAttachment = NSTextAttachment()
    let textAttachment = NSTextAttachment()
//let imageStyle = NSMutableParagraphStyle()
    let imageStyle = NSMutableParagraphStyle()
    //imageStyle.alignment = .center
    imageStyle.alignment = .center
    //textAttachment.image = image
    textAttachment.image = image
    let imageText = NSAttributedString(attachment: 
    textAttachment).mutableCopy() as! NSMutableAttributedString
    //attempt to center image...
    let attributedStringToAppend: NSMutableAttributedString = 
    NSMutableAttributedString(attributedString: 
    NSAttributedString(string: "\n\n"))

    //attributedStringToAppend.addAttributes(attr, range: range)

    let combination2: NSMutableAttributedString = 
    NSMutableAttributedString(attributedString: 
    NSAttributedString(string: "\n\n"))


    combination2.append(lineBreak)
    // combination2.addAttribute(attr, range: NSRange(location: 0, 
    length: length))

    combination2.append(imageText)
    //this text will display the "Walktrough" text on image 

    combination2.append(attributedText)

    tv.attributedText = combination2

person CVar    schedule 25.01.2019    source источник
comment
Вы должны использовать стиль абзаца, который вы создали. Код в этом вопросе делает именно это.   -  person rmaddy    schedule 25.01.2019
comment
Но я уже пробовал это так... все равно xcode выдает мне ошибки...   -  person CVar    schedule 25.01.2019
comment
Код в вашем вопросе не пытается центрировать изображение, и он не похож на код, который я связал. Обновите свой вопрос, указав свой код.   -  person rmaddy    schedule 25.01.2019


Ответы (1)


введите описание изображения здесьОтвет (у меня сработало!):

    let paragraphStyle = NSMutableParagraphStyle()

    paragraphStyle.alignment = .center

    attributedText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: length))

    let size = CGSize(width: 100, height: 100)


    //Here is an attempt to resize image and center it...



    let image = ResizeImage(image: UIImage(named: "logo")!.withRenderingMode(.alwaysTemplate), targetSize: size)


    let textAttachment = NSTextAttachment()


    let imageStyle = NSMutableParagraphStyle()


    imageStyle.alignment = .center


    textAttachment.image = image


    let imageText = NSAttributedString(attachment: textAttachment).mutableCopy() as! NSMutableAttributedString

    let length2 = imageText.length

    imageText.addAttribute(NSAttributedString.Key.paragraphStyle, value: imageStyle, range: NSRange(location: 0, length: length2))
person CVar    schedule 27.01.2019