最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

cocoa - Set Rendering Attributes with NSTextLayoutManager - Stack Overflow

programmeradmin2浏览0评论

I have a NSTextContentStorage and NSTextLayoutManager hooked up to render text using TextKit 2. At times, I'd like to render certain attributes (like font color) for selected text ranges different from what is on the attributed string on NSTextContentStorage.

func setRenderingAttributes(_ renderingAttributes: [NSAttributedString.Key : Any], for textRange: NSTextRange ) seems to be a good fit.

I have tried

textLayoutManager.setRenderingAttributes(renderAttributeOverride.attribute, for: renderAttributeOverride.textRange)
textLayoutManager.textViewportLayoutController.layoutViewport()

But, it doesn't seem to have any affect on the layout fragments generated by the layoutManager. The lineFragments in the layoutFragment have attributedStrings that correspond to the attributes on NSTextContentStorage.

I have also tried doing the setRenderingAttributes inside a renderingAttributesValidator to no apparent effect

textLayoutManager.renderingAttributesValidator = { [weak self] textLayoutManager, textLayoutFragment in
                guard let weakSelf = self else { return }
                let fragmentRange = textLayoutFragment.rangeInElement

                for renderAttributeOverride in weakSelf.renderAttributeOverrides {
                    if fragmentRange.intersects(renderAttributeOverride.textRange) {
                       textLayoutManager.setRenderingAttributes(renderAttributeOverride.attribute, for: renderAttributeOverride.textRange)
                    }
                }
            }

How do we go about using this method?

发布评论

评论列表(0)

  1. 暂无评论