2

I've create a UITextView that I've added to a UIView, this TextView should display an image at the top right or left corner wrapped by some text.

Creating the TextView isn't the issue (obviously), I just can't wrap my head around the NSAttributedString.

As for the image, I've used a NSTextAttachment;

var image:UIImage = UIImage(named: "placeHolder.png")
var textAttachment:NSTextAttachment = NSTextAttachment(data: nil, ofType: nil)
textAttachment.image = image

Then we've got some text for the TextView;

var text:String = "Lorem ipsum"

Now the tricky part. I can create an NSAttributedString that can be set to the UITextView. It displays the image just fine, I just can't figure out how to set the text.

var s:NSAttributedString = NSAttributedString(attachment: textAttachment)
textView.attributedText = s

Any ideas?

AlBirdie
  • 2,071
  • 1
  • 25
  • 45

3 Answers3

1

Here's code that shows how to construct an NSAttributedString consisting of text and some NSTextAttachments:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch10p507tabStops/TextTabTest/ViewController.swift

However...

this TextView should display an image at the top right or left corner wrapped by some text.

Then what you want is not an NSTextAttachment, since that is an inline image without wrapping.

To get an image at a fixed position with wrapping, use an image subview and cause the wrapping by using an exclusion path.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • D'oh! Nice job wasting one hour with the NSTextAttachment Al. :) Thanks for the link @matt, might become handy for something else in the future. The exclusion path does look a little more promising for this one, though. :P – AlBirdie Aug 27 '14 at 16:55
  • I suggest you watch the WWDC 2013 videos about TextKit, since they specifically talk about wrapping around a fixed image. – matt Aug 27 '14 at 17:02
0

To set the text before the image:

var attributeString:NSMutableAttributedString = NSMutableAttributedString(string: text)
attributeString.appendAttributedString(s)
textView.attributedText = attributeString;

Or the image before the text:

var attributeString:NSMutableAttributedString = NSMutableAttributedString(string: s)
s.appendAttributedString(text)
textView.attributedText = s
Carlos Irano
  • 682
  • 7
  • 8
0

I created an example project for this question. I also used stringAttributes for the text.

You can refer with it.

Here is the code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let imageSize = CGSize(width: 20, height: 20)

        let finalAttributedString = NSMutableAttributedString(string: "")

        let birdStringAttributes = [
            NSForegroundColorAttributeName: UIColor.greenColor(),
            NSBackgroundColorAttributeName: UIColor.blueColor(),
            NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
            NSFontAttributeName: UIFont.systemFontOfSize(19.0)
        ]

        finalAttributedString.appendAttributedString(stringForAttachment(named: "bird", imageSize: imageSize, caption: "Something really amazing happened in Downtown Spokane this week and I had to share the story with you. Some of you may know that my brother, Joel, is a loan officer at Sterling Bank. He works downtown in a second story office building, overlooking busy Riverside Avenue.", stringAttributes: birdStringAttributes))

        let duckStringAttributes = [
            NSForegroundColorAttributeName: UIColor.orangeColor(),
            NSBackgroundColorAttributeName: UIColor.blueColor(),
            NSFontAttributeName: UIFont.systemFontOfSize(14.0)
        ]

        finalAttributedString.appendAttributedString(stringForAttachment(named: "duck", imageSize: imageSize, caption: "Several weeks ago he watched a mother duck choose the cement awning outside his window as the uncanny place to build a nest above the sidewalk.The mallard laid ten eggs in a nest in the corner of the planter that is perched over 10 feet in the air. She dutifully kept the eggs warm for weeks and Monday afternoon all of her ten ducklings hatched.", stringAttributes: duckStringAttributes))

        label.attributedText = finalAttributedString
    }

    private func stringForAttachment(named imageName: String, imageSize: CGSize, caption: String, stringAttributes: [String : AnyObject]) -> NSAttributedString {
        let attachment = NSTextAttachment()
        let image = UIImage(named: imageName)
        attachment.image = image
        attachment.bounds.size = imageSize
        let fullString = NSMutableAttributedString(string: "")
        fullString.appendAttributedString(NSAttributedString(attachment: attachment))
        fullString.appendAttributedString(NSAttributedString(string: caption, attributes: stringAttributes))
        return fullString
    }

}

Result is:

enter image description here

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116