简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM