NSAttribute 不会使 Bold Swift 变得粗体
我正在尝试使用boldFullName 函数将全名设为粗体。但显然,它并没有对其做出任何改变。我相信转换为字符串正在删除 mutableString 功能。我怎样才能在不返回 NSAttributedString 的情况下避免它
class NewNotificationModel: Serializable {
var fromUser: NewNotificationFromUserModel!
}
class NewNotificationFromUserModel: Serializable {
var name: String!
var lastName: String!
}
final class NewNotificationViewModel {
// MARK: Properties
var notificationModel: NewNotificationModel!
private(set) var fullName: String!
init(notificationModel: NewNotificationModel) {
self.notificationModel = notificationModel
self.fullName = boldFullName(getFullName())
private func getFullName() -> String {
guard let fromUser = notificationModel.fromUser, let name = fromUser.name, let lastname = fromUser.lastName else { return "" }
return name + " " + lastname
}
func boldFullName(_ fullname: String) -> String {
let range = NSMakeRange(0, getFullName().count)
let nonBoldFontAttribute = [NSAttributedString.Key.font:UIFont.sfProTextSemibold(size: 16)]
let boldFontAttribute = [NSAttributedString.Key.font:UIFont.catamaranBold(size: 20)]
let boldString = NSMutableAttributedString(string: getFullName() as String, attributes:nonBoldFontAttribute)
boldString.addAttributes(boldFontAttribute, range: range)
return boldString.mutableString as String
}
}
并且我在表格单元格中使用这个全名,如下所示
class NewNotificationTableViewCell: UITableViewCell, Reusable, NibLoadable {
@IBOutlet weak var messageLbl: UILabel!
messageLbl.text = NewNotificationTableViewCell.configureText(model: model)
我的 configureText 函数是
private static func configureText(model: NewNotificationViewModel) -> String {
guard let type = model.type else { return "" }
switch NotificationType.init(rawValue: type) {
String(format:"new_notification.group.request.want.join.text_%@_%@".localized, model.fullName, model.groupName ?? "")
case .mention?: return String(format:"new_notification.post.mention_%@".localized, model.fullName)
但是那些 .fullName 对加粗 fullName 没有任何作用。
编辑为 NSAttributedString 但这会导致
case .internalCommunicationMessage?: return NSAttributedString(format:("new_notification.announcement.text_%@".localized), args: NSAttributedString(string: model.groupName ?? ""))
此扩展出错。
public extension NSAttributedString {
convenience init(format: NSAttributedString, args: NSAttributedString...) {
let mutableNSAttributedString = NSMutableAttributedString(attributedString: format)
args.forEach { (attributedString) in
let range = NSString(string: mutableNSAttributedString.string).range(of: "%@")
mutableNSAttributedString.replaceCharacters(in: range, with: attributedString)
}
self.init(attributedString: mutableNSAttributedString)
}
无法
将类型“String”的值转换为预期的参数类型“NSAttributedString”
I am trying to make fullname bold with boldFullName func. But obviously, it does not make any change on it. I believe that casting to string is deleting mutableString features. How can I avoid it without returning NSAttributedString
class NewNotificationModel: Serializable {
var fromUser: NewNotificationFromUserModel!
}
class NewNotificationFromUserModel: Serializable {
var name: String!
var lastName: String!
}
final class NewNotificationViewModel {
// MARK: Properties
var notificationModel: NewNotificationModel!
private(set) var fullName: String!
init(notificationModel: NewNotificationModel) {
self.notificationModel = notificationModel
self.fullName = boldFullName(getFullName())
private func getFullName() -> String {
guard let fromUser = notificationModel.fromUser, let name = fromUser.name, let lastname = fromUser.lastName else { return "" }
return name + " " + lastname
}
func boldFullName(_ fullname: String) -> String {
let range = NSMakeRange(0, getFullName().count)
let nonBoldFontAttribute = [NSAttributedString.Key.font:UIFont.sfProTextSemibold(size: 16)]
let boldFontAttribute = [NSAttributedString.Key.font:UIFont.catamaranBold(size: 20)]
let boldString = NSMutableAttributedString(string: getFullName() as String, attributes:nonBoldFontAttribute)
boldString.addAttributes(boldFontAttribute, range: range)
return boldString.mutableString as String
}
}
And I am using this fullname in table cell as below
class NewNotificationTableViewCell: UITableViewCell, Reusable, NibLoadable {
@IBOutlet weak var messageLbl: UILabel!
messageLbl.text = NewNotificationTableViewCell.configureText(model: model)
My configureText func is
private static func configureText(model: NewNotificationViewModel) -> String {
guard let type = model.type else { return "" }
switch NotificationType.init(rawValue: type) {
String(format:"new_notification.group.request.want.join.text_%@_%@".localized, model.fullName, model.groupName ?? "")
case .mention?: return String(format:"new_notification.post.mention_%@".localized, model.fullName)
But those .fullName does not do anything about bolding fullName.
Edited as NSAttributedString but this gives error
case .internalCommunicationMessage?: return NSAttributedString(format:("new_notification.announcement.text_%@".localized), args: NSAttributedString(string: model.groupName ?? ""))
with this extension.
public extension NSAttributedString {
convenience init(format: NSAttributedString, args: NSAttributedString...) {
let mutableNSAttributedString = NSMutableAttributedString(attributedString: format)
args.forEach { (attributedString) in
let range = NSString(string: mutableNSAttributedString.string).range(of: "%@")
mutableNSAttributedString.replaceCharacters(in: range, with: attributedString)
}
self.init(attributedString: mutableNSAttributedString)
}
}
Cannot convert value of type 'String' to expected argument type 'NSAttributedString'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
主要问题是您试图将
attributedString
赋予text
属性,这不会影响UILabel
。您必须将代码的某些部分更改为 :到 :
和
to:
最后当您调用时使用
attributedText
而不是string
The main problem is you are trying to give
attributedString
totext
property which is not gonna effect on theUILabel
. You must change some part of your code like :to :
And
to:
And last when you call use
attributedText
instead ofstring
使用此扩展,
我修改了我的开关案例,如下所示,
更改了configureText的返回类型
,删除了boldFullName函数,并将fullName类型更改回String
,最后插入到标签中,如下所示。
with this extension
I modified my switch cases as below
the changed return type of configureText
deleted boldFullName function and changed fullName type back to String
and finally inserted as below to label.
字符串不包含属性,您确实需要从函数返回一个
NSAttributedString
。您可以做的是将属性字符串分配给
UILabel
的attributedText
属性。文档此处示例(更新函数以返回
NSAttributedString
):String doesn't contain attributes, you do need to return a
NSAttributedString
from your function.What you can do instead is assigning the attributed string to the
attributedText
property of yourUILabel
. Documentation hereExample (after updating your function to return a
NSAttributedString
):您需要分配
messageLbl.attributedText
You need to assign
messageLbl.attributedText