Search code examples
iosnsdictionarynsattributedstringswiftxcode6.3

How can I set the attributed text while having no placeholder text?


Code that worked in Xcode 6.2 no longer works and I'm having trouble figuring out how to fix it. String attributes are now ignored unless I set default text. Here's what I've got right now:

    let font = UIFont(name: "Arial-BoldMT", size: 42.0)!
    let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
    textStyle.alignment = NSTextAlignment.Center
    let textColor = UIColor.whiteColor()
    var shadow = NSShadow()
    shadow.shadowColor = UIColor.blackColor()
    shadow.shadowOffset = CGSizeMake(2.0,2.0)

    var attr = [String:NSObject]()

    attr = [
        NSFontAttributeName: font,
        NSForegroundColorAttributeName: textColor,
        NSParagraphStyleAttributeName: textStyle,
        NSShadowAttributeName: shadow
    ]

    let placeholderText = NSAttributedString(string: "", attributes: attr)
    textView.attributedText = placeholderText

Solution

  • If you change the placeHolderText to NSAttributedString(string: " ", attributes: attr) you can then change the UITextView's text by changing its text property and the new text will have all the same attributes. I hope that answers your question.