如何在 Swift 中使用属性字符串附加属性文本字符串 [英] how to append Attributed Text String with Attributed String in Swift

查看:78
本文介绍了如何在 Swift 中使用属性字符串附加属性文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Swift 中附加一个属性文本和另一个属性文本.请提供任何示例代码,用于在 Swift 中对两个属性字符串进行附加操作.

I want to append an Attributed Text with another Attributed Text in Swift. Please provide any sample code for appending operation of two attributed String in Swift.

推荐答案

使用 NSMutableAttributedString

Use NSMutableAttributedString

小例子

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) 

斯威夫特 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 表示您的最终字符串,其中包含 yourAttributesyourOtherAttributes

combination represents your final string which contains both formattings provided by yourAttributes and yourOtherAttributes

这篇关于如何在 Swift 中使用属性字符串附加属性文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆