risa8
risa8

Reputation: 253

Add line spacing to textView not working

I would like to add line spacing to textView. With the following code I get an error "textView has no member attributedText". How can I solve this problem?

import UIKit

@objc protocol TextViewCellProtocol:NSObjectProtocol {
    func textViewCellDidTouchedButton(_ cell:TextViewCell)
}

class TextViewCell: UICollectionViewCell {

     @IBOutlet var textView:FuriganaTextView!

     let style = NSMutableParagraphStyle()
     style.lineSpacing = 40
     let attributes = [NSParagraphStyleAttributeName : style]
     textView.attributedText = NSAttributedString(string: textView.text,
                                                             attributes: attributes)
}

Upvotes: 1

Views: 270

Answers (2)

Baig
Baig

Reputation: 4995

FuriganaTextView is build over TextKit not a subclass of UITextView. YOu can set attributeed string by using furiganaTextView.contents property. Here is usage example.

// Prepare furigana contents
// Note: The order of the furiganas provided to the FuriganaTextView is important.
// It must be ascending in manner of the location of the range,
// otherwise the furiganas may not be rendered at the correct position.
let furiganas = [
  Furigana(text: "た", original: "田", range: NSMakeRange(0, 1)),
  Furigana(text: "なか", original: "中", range: NSMakeRange(1, 1)),
]
let contents = NSAttributedString(string: "田中さん、中華料理を食べたことありますか。")

// Tell FuriganaTextView about 
// the furiganas (which is an array of the Furigana struct) 
// and the contents to display (a NSAttributedString)
furiganaTextView.furiganas = furiganas
furiganaTextView.contents = contents

// To customize the text view, use the contentView
// contentView will return a valid UITextView after
// the contents has been set
furiganaTextView.contentView?.backgroundColor = UIColor.lightGray

Upvotes: 0

Aravind A R
Aravind A R

Reputation: 2714

I believe you are getting the error because the textView class that you are using (FuriganaTextView) doesn't have a member attributedText. After checking their gitHub documentation. I guess you could add attributed text to FuriganaTextView by specifying the attributed string to the contents property of FuriganaTextView like

textView.contents = NSAttributedString(string: textView.text,
                                                         attributes: attributes)

Upvotes: 1

Related Questions