ios - 如何使用 swift 为 Xcode 中的特定文本添加颜色?
问题描述
我在通过在 Xcode 中进行 API 调用收到的字符串末尾附加了一个文本。现在,我想添加颜色并仅将“点击阅读更多”文本加粗。关于如何实现这一目标的任何建议?感谢您的帮助:)
self.content[i] = recievedData.title! + "\nTap to Read More "
let str = "Tap to Read More"
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.red,
]
let attributedQuote = NSAttributedString(string: str, attributes: attributes)
解决方案
你可以试试NSAttributedString
。
例子:
let str = "Tap to Read More"
// Create your attributed string
let attributes: [NSAttributedString.Key : Any] = [.foregroundColor : UIColor.red]
let attributedStr = NSAttributedString(string: str, attributes: attributes)
// Example for a UILabel
let lbl = UILabel()
lbl.attributedText = attributedStr
文档NSAttributedString
:https ://developer.apple.com/documentation/foundation/nsattributedstring
推荐阅读
- javascript - Javascript中的第一类函数和回调函数有什么区别吗?
- php - 在laravel中更改与ckeditor5集成的ckfinder3的默认窗口
- ios - 如何在 iOS 应用中获取 Wifi 提供商信息
- python - 如何在 pygame 中将 f 字符串传送到 if 语句?
- conditional-operator - 与三元运算符混淆
- firebase - 如何在 Flutter 中使用 FutureBuilder 小部件更新数据?
- react-native - 异步/等待 componentDidMount 中的数据获取未按预期提供结果
- google-app-engine - 使用 gcloud 命令行界面在特定区域创建谷歌云应用引擎实例?
- python - 我正在尝试使用 py2app 构建我的 python 代码的独立应用程序,但是当我打开应用程序时出现错误
- python - Python - 准备向频道发送消息的不和谐机器人