Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build an Interactive Story App with Swift Creating the User Interface Programmatically Attributed Strings

Qasa Lee
Qasa Lee
18,915 Points

for Xcode 9 and Swift 4 there's a change we may encounter:

'NSParagraphStyleAttributeName' has been renamed to 'NSAttributedStringKey.paragraphStyle'

Hope this might help you.

Thanks a lot :)

3 Answers

Now it's 'NSAttributedString.Key.paragraphStyle'. (Add a "." before "Key").

Trevor Duersch
Trevor Duersch
9,964 Points

Is it just me, or is part of the text cut off? After I made that change (thanks, by the way!), the text only goes to "from the surface of Mars. It's" and stops there. This is what code I have

            let attributedString = NSMutableAttributedString(string: page.story.text)

            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 10

            attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))

            storyLabel.attributedText = attributedString

Anybody else seeing this? Or will it get resolved in the future videos?

Jonathan Mazala
Jonathan Mazala
9,860 Points

you need to set storyLabel.numberOfLines = 0 after adding it as a subview

Joey Liu
Joey Liu
3,982 Points

Is it just me, after I added the code . Parsan requested as below:

if let page = page { artworkView.image = page.story.artWork

        let attributeString = NSMutableAttributedString(string: page.story.text)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 10

        attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributeString.length))

        storyLabel.attributedText = attributeString

There is a Green Thread break point 1.1 at paragraphStyle = NSMutableParagraphStyle()