添加行间距到textView无法正常工作

时间:2017-09-06 06:05:52

标签: ios swift

我想在textView中添加行间距。使用以下代码我得到一个错误“textView没有成员attributesText”。 我该如何解决这个问题?

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)
}

2 个答案:

答案 0 :(得分:1)

我相信您收到错误,因为您正在使用的textView类(FuriganaTextView)没有成员attributedText。检查完gitHub documentation后。我猜您可以通过将FuriganaTextView contents属性的属性字符串指定为<{p}},将属性文本添加到FuriganaTextView

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

答案 1 :(得分:0)

FuriganaTextView是在TextKit上构建的,而不是UITextView的子类。你可以使用furiganaTextView.contents属性设置属性字符串。这是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