Ulli H Ulli H - 1 year ago 105
Swift Question

Can't store data to a var (property) of a class

I know, my title is confusing, but my problem is confusing for me, too. :-(

In the class ReadMoreTextView I've a var of the type NSAttributedString that I can fill but when I read it, it is nil!?
Ok, I've to declare this and that with my terrible English ;-)

I want to use a IMHO very nice solution "ReadMoreTextView" of Ilya Puchka to "compress" a large UITextView:


I want to use an

, because I want to "highlight" the "Read more" with a red or blue color.

For that I use this init:

import UIKit
class ViewController: UIViewController {

@IBOutlet var textView: ReadMoreTextView!
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
let text = "und ein neuer text Lorem ipsum dolor..."

let attributedString = NSAttributedString(string:text as String)
// 1) 2) 3)
textView = ReadMoreTextView(maximumNumberOfLines: 3, attributedTrimText: attributedString, shouldTrim: true)


  1. are the Number of the leading lines in the "compressed" view,

  2. is the NSAttributedString and

  3. is a Flag, if the Text should be trimmed

And here is the class:

class ReadMoreTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
scrollEnabled = false
editable = false

convenience init(maximumNumberOfLines: Int, attributedTrimText: NSAttributedString?, shouldTrim: Bool) {
self.maximumNumberOfLines = maximumNumberOfLines // 1)
self.attributedTrimText = attributedTrimText // 2)
println(attributedTrimText) // of course displays the attributedTrimText
self.shouldTrim = shouldTrim // 3)

var maximumNumberOfLines: Int = 0 {
didSet { setNeedsLayout() }

var attributedTrimText: NSAttributedString? {
didSet { setNeedsLayout() }

var shouldTrim: Bool = false {
didSet { setNeedsLayout() }

Then I make a "po attributedTrimText" at a breakpoint at shouldTrim (last called), the result is nil?!

So because of the nil in attributedTrimText, the textView is empty when it will be displayed! :-(

I can't find the reason...

Answer Source

If you've set the class of the textView in the Storyboard to ReadMoreTextView, then the Storyboard instantiates the ReadMoreTextView class for you and assigns it to the textView IBOutlet.

On this line of code:

textView = ReadMoreTextView(maximumNumberOfLines: 3, attributedTrimText: attributedString, shouldTrim: true)

you are creating a new ReadMoreTextView and overwriting your outlet pointer textView pointing to this new ReadMoreTextView. Unfortunately, now you are not interacting with the ReadMoreTextView that is on screen.

You shouldn't overwrite an IBOutlet variable. You should just use it to read/write the properties of the IBOutlet variable.

textView.maximumNumberOfLines = 3
textView.attributedTrimText = attributedString
textView.shouldTrim = true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download