ios - NSAttributedString 未返回预期值
问题描述
我正在尝试创建如下文本:“比尔开始关注你。” 并使用户名半粗体。我试过了,但是 .string 给了我一个奇怪的长字符串。我该如何解决?
//let nUsername = username
let fontU = UIFont(name: "SFCompactDisplay-Semibold", size: 15)
let fontN = UIFont(name: "SFCompactDisplay-Regular", size: 15)
let usernameAttributes = [NSAttributedString.Key.font: fontU]
let notifAttributes = [NSAttributedString.Key.font: fontN]
let firstString = NSMutableAttributedString(string: username, attributes: usernameAttributes)
let secondString = NSAttributedString(string: " started following you.", attributes: notifAttributes)
firstString.append(firstString)
firstString.append(secondString)
//notifInfoLabel.frame = CGRect(x: 77, y: 15, width: 295, height: 18)
notifInfoLabel.text = "\(firstString.string)"
解决方案
摆脱:
firstString.append(firstString)
这重复了用户名。
而且您没有使用最终的属性字符串。利用:
notifInfoLabel.attributedText = firstString
推荐阅读
- r - 在 R 中使用 openxlsx 写入数据后如何保持样式格式不变
- c++ - MinGW 编译器拒绝访问文件?
- c++ - 在 C++ 项目中使用 clang 作为库
- php - Wordpress SQL 查询和符号
- python - 如何创建具有指定特征的类?
- swift - 如何在运行时选择正确的 Int 类型
- python - 运行我的 discord.py 机器人时在 client.run('token') 收到运行时错误
- angular - 由于某种原因,Angular 8 中的 POST 无法正常工作
- java - 如何将字符串 yyyy-MM-ssThh-mm-ss 转换为 LocalDataTime yyyy-MM-ss hh-mm-ss?
- next.js - 如何在 Next.js 中的 Link 之前添加一个空格?