如何在 Swift 中将属性文本字符串附加到属性字符串

2024-01-02

我想在 Swift 中附加一个属性文本和另一个属性文本。请提供在 Swift 中添加两个属性字符串的示例代码。


Use NSMutableAttributedString为了实现这一点。

Example

Swift 5

let yourAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

partOne.append(partTwo) 

Swift 3

let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

let combination = NSMutableAttributedString()

combination.append(partOne)
combination.append(partTwo)

combination代表您的最终字符串,其中包含由yourAttributes and yourOtherAttributes

甚至更老

let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

let combination = NSMutableAttributedString()

combination.appendAttributedString(partOne)
combination.appendAttributedString(partTwo) 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Swift 中将属性文本字符串附加到属性字符串 的相关文章

随机推荐

  • 是否可以设置 TextBoxFor 生成的输入类型

    我在表单中使用 ASP NET MVC 3 TextBoxFor 并希望使用 type email 以便至少在某些移动设备上更轻松地输入 但找不到如何使用 TextBoxFor 设置它 这不是很容易就能实现的吗 In View Html L
  • 如何在 flutter 中使用 getx 使列表可观察

    我正在观看有关使用 Getx 和 API 的教程 设置用于获取数据的控制器后 我们声明了一个列表变量并使其可观察 obs 但列表格式已被弃用 你能帮我吗我现在该怎么做 var products List
  • 无法使用 IIS 7 加载图像

    我正在构建一个新的 Web 服务器 并且正在重新部署一个在 IIS6 中正常工作的 ASP Net 站点 不幸的是 没有任何图像正在加载 我收到了 404 错误 图像位于正确的文件夹中 我什至可以导航到网站中的正常页面 有什么建议么 可能是
  • Seabornpairplot 色调参数未按预期工作

    如果将 C 指定为 hue 参数的值 则预计 Seaborn 不会显示 C 列 我错了吗 sns pairplot df hue C 数据框 默认情况下 seaborn 将显示所有数字列 因此 如果您的 色调 列 在您的情况下为 C 列为字
  • 为 highchart 中的两个系列之一设置 minPointLength

    我的高图中有两个数据系列 并且只想对其中一个使用 minPointLength 这个可以设置吗 我的系列仅包含 名称 颜色和数据 我的图表配置如下所示 const chartConfig chart type column dashStyl
  • 如何安装源自 apt 包的 Python 绑定?

    我有一个网站托管在Heroku http heroku com 我现在想使用python qrtools包使用了ZBar条码扫描器 http zbar sourceforge net 在常规的 debian 基于 上 我可以做一个简单的操作
  • 为什么我无法在另一个文件中定义内联成员函数?

    我有三个文件 1 Joy h class Joy public void test 2 Joy cpp include Joy h inline void Joy test 3 main cpp include Joy h int main
  • 如何解决访问 CollabNet Subversion 控制台时出现 503 错误?

    我正在使用 Collabnate Subversion Edge 尝试访问 URL 时出现以下错误http localhost 3343 csvn http localhost 3343 csvn Collbnate 在 Windows S
  • 将 git 存储库移至第二台计算机?

    我有一个带有简单本地 git 存储库的项目 我想要move将该项目 文件夹等 转移到另一台计算机 并从现在开始从那里开始工作 我不想在旧机器上留下任何东西 当然除了我的其他 git 项目 我希望它就像我一直在新机器上工作一样 我可以简单地将
  • 405(不允许的方法)Laravel

    我在 Laravel 中尝试使用 ajax 删除项目时收到 405 方法不允许 请有人帮忙 这是我的路线 Route get home HomeController index gt name home Route post destroy
  • long long 在 32 位机器上的实现

    根据c99标准 尺寸为long long至少应为 64 位 这是如何在 32 位机器中实现的 例如 2 的加法或乘法 long longs 另外 什么相当于long long in C C 中的等效项也是 long long 标准并不要求它
  • 客户端无法连接到本地主机上的 RabbitMQ 服务器

    我通过 apt get 在 ubuntu 12 04 2 LTS 上安装了新的 RabbitMQ 3 1 3 并尝试在同一台服务器上启动消费者 但我遇到了连接问题 PhpAmqpLib Exception AMQPRuntimeExcept
  • Android studio java.lang.SecurityException:需要 INSTALL_LOCATION_PROVIDER 权限

    我正在android工作室工作 我正在移动应用程序上获取实时 GPS 坐标 在每个移动设备上都会生成坐标 但单个移动设备无法工作并给出zero 0 坐标有android版本9 下面是我的代码 SuppressLint MissingPerm
  • 使用 Promise 的 Lambda 的 sinon 存根

    我刚刚开始使用 sinon 并且在消除 DynamoDB 调用方面取得了一些初步成功 sandbox sinon createSandbox update stub sandbox stub AWS DynamoDB DocumentCli
  • iOS 测试/规范 TDD/BDD 以及集成和验收测试

    Locked 这个问题及其答案是locked help locked posts因为这个问题是题外话 但却具有历史意义 目前不接受新的答案或互动 在 iPhone 上进行行为驱动开发的最佳技术是什么 有哪些开源示例项目可以展示这些技术的合理
  • 如何通过JMX暴露Hystrix的断路器状态

    我一直在寻找有关如何在 JMX 上公开 Hystrix 断路器状态的教程 我刚刚发现了一个用于公开指标 例如计数器 仪表等 的 API 使用hystrix servo metrics publisher 是否可以在 JMX 上公开断路器状态
  • 在类中访问向量

    如果我的班级中有一个向量作为私有成员 那么访问它的最佳方式是什么 例如 采用以下简单的类 class MCL private std vector my vec public Include constructor here and oth
  • 使用 django-tables2 显示页码

    我目前正在使用 django tables2 显示数据集 文档没有特别提到这一点 所以我猜这可能需要一些表覆盖 但是 我希望有人已经完成了这一点 如何使用表格下方的 django tables2 呈现页码 我希望能够显示的是用户可以单击的页
  • 如何使用 sourcetree 进行恢复?

    如果您使用 sourcetree 恢复到先前提交的过程是什么 请不要使用命令行帮助 我了解如何在那里实现相同的结果 但我特别希望使用我友好的 atlassian 工具来实现这一点 你所说的恢复到底是什么意思 您可以提交一个反向提交右键单击提
  • 如何在 Swift 中将属性文本字符串附加到属性字符串

    我想在 Swift 中附加一个属性文本和另一个属性文本 请提供在 Swift 中添加两个属性字符串的示例代码 Use NSMutableAttributedString为了实现这一点 Example Swift 5 let yourAttr