我试图定义在我的文字中发生软换行的位置,而NSLayoutManagerDelegate
方法layoutManager(_:shouldBreakLineByWordBeforeCharacterAt:)
似乎就是开始的地方。我的问题是它没有被调用,虽然我的其他方法是。这是在基本的UITextView
设置下进行的。在什么条件下这种方法被称为 - 我在正确的位置寻找吗?
这是我的TextKit堆栈的样子:
class DemoTextView: UITextView, NSLayoutManagerDelegate {
// MARK: - Init
required init() {
let textStorage = NSTextStorage(string: "i will overflow several lines probably just saying")
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: CGSize(width: 100, height: 100))
textContainer.lineBreakMode = .byWordWrapping
textContainer.lineFragmentPadding = 0
layoutManager.addTextContainer(textContainer)
// Super
super.init(frame: .zero, textContainer: textContainer)
self.textContainerInset = .zero
// Configure Text View
layoutManager.delegate = self
self.isScrollEnabled = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Layout Manager
func layoutManager(_ layoutManager: NSLayoutManager, shouldBreakLineByWordBeforeCharacterAt charIndex: Int) -> Bool {
print(#function)
return false
}
func layoutManager(_ layoutManager: NSLayoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt charIndex: Int) -> Bool {
print(#function)
return false
}
}
答案 0 :(得分:1)
我在实际应用中尝试了您的代码,文本视图占据整个超级视图,如果初始字符串很长,则在启动时多次调用shouldBreakLineByWordBeforeCharacterAt
,然后每次用户键入字符时再次调用na.omit(gifts[gifts < Date_3])
- 完全符合预期。