iphone - 使用 Core Text 为给定的字符串范围设置背景颜色/突出显示颜色

标签 iphone ios highlight nsattributedstring core-text

我在我的 iPhone 应用程序中使用 Core Text 布置了一些文本。我正在使用 NSAttributedString 为给定范围的文本设置某些样式。

我似乎找不到用于设置背景/突出显示颜色的属性,尽管它似乎是可能的。我找不到听起来相关的属性名称常量,文档仅列出:

kCTCharacterShapeAttributeName
kCTFontAttributeName
kCTKernAttributeName
kCTLigatureAttributeName
kCTForegroundColorAttributeName
kCTForegroundColorFromContextAttributeName
kCTParagraphStyleAttributeName
kCTStrokeWidthAttributeName
kCTStrokeColorAttributeName
kCTSuperscriptAttributeName
kCTUnderlineColorAttributeName
kCTUnderlineStyleAttributeName
kCTVerticalFormsAttributeName
kCTGlyphInfoAttributeName
kCTRunDelegateAttributeName

Twitterrific 的开发者 Craig Hockenberry 曾在 Twitter 上公开表示,他使用 Core Text 来呈现推文,而 Twitterrific 具有我在触摸链接时谈论的背景/亮点。

alt text

任何正确方向的帮助或指示都会很棒,谢谢。

编辑:这是一个link to the tweet Craig 发帖提到“核心文本、属性字符串和大量辛勤工作”,以及 follow up提到使用 CTFrameSetter 指标来计算触摸是否与链接相交。

最佳答案

最后,我不得不使用 Core Text 指标方法(获取范围、边界、线的原点等)来计算链接的边界矩形。链接跨越的每一行都有一个矩形后,我将背景绘制到上下文中,然后再重绘其上方的文本。

看起来工作量很大,但确实有效。

关于iphone - 使用 Core Text 为给定的字符串范围设置背景颜色/突出显示颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498918/

相关文章:

ios - 多个 EGOImageView 出现低内存警告

javascript - 如何在横向网页的导航栏中突出显示?

C# - 如何在字符串中查找字符串,即使它跨越空白?

ios - 如何让 slider 以 0.25 为步长移动?

ios - 获取所选月份的天数列表

vim - Vim 中的代码折叠/缩进指南?

ios - 数组计数的 UITableView 问题

php - 添加到主屏幕 Web 应用程序

iphone - 带有自定义控件的 MPMoviePlayerController

iphone - 关于如何设置 Storyboard以使元素不重叠的建议