添加行间距到textView不起作用

时间:2022-05-06 23:17:20

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?

我想为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 个解决方案

#1


1  

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类(FuriganaTextView)没有成员attributionText。检查了他们的gitHub文档。我想你可以通过在FuriganaTextView的contents属性中指定属性字符串来将属性文本添加到FuriganaTextView

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

#2


0  

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

FuriganaTextView是在TextKit上构建的,而不是UITextView的子类。你可以使用furiganaTextView.contents属性设置属性字符串。这是用法示例。

// 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

#1


1  

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类(FuriganaTextView)没有成员attributionText。检查了他们的gitHub文档。我想你可以通过在FuriganaTextView的contents属性中指定属性字符串来将属性文本添加到FuriganaTextView

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

#2


0  

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

FuriganaTextView是在TextKit上构建的,而不是UITextView的子类。你可以使用furiganaTextView.contents属性设置属性字符串。这是用法示例。

// 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