3 回答
TA贡献1851条经验 获得超3个赞
如果您使用的是本地化字符串,则可能无法始终依靠粗体字符串作为句子的结尾。如果是这种情况,则可以很好地进行以下操作:
例如查询“ blah”与任何项目都不匹配
/* Create the search query part of the text, e.g. "blah".
The variable 'text' is just the value entered by the user. */
let searchQuery = "\"\(text)\""
/* Put the search text into the message */
let message = "Query \(searchQuery). does not match any items"
/* Find the position of the search string. Cast to NSString as we want
range to be of type NSRange, not Swift's Range<Index> */
let range = (message as NSString).rangeOfString(searchQuery)
/* Make the text at the given range bold. Rather than hard-coding a text size,
Use the text size configured in Interface Builder. */
let attributedString = NSMutableAttributedString(string: message)
attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(label.font.pointSize), range: range)
/* Put the text in a label */
label.attributedText = attributedString
- 3 回答
- 0 关注
- 580 浏览
添加回答
举报