NSAttribute 不会使 Bold Swift 变得粗体

发布于 2025-01-16 12:41:50 字数 2925 浏览 1 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肥爪爪 2025-01-23 12:41:54

主要问题是您试图将 attributedString 赋予 text 属性,这不会影响 UILabel 。您必须将代码的某些部分更改为 :

private(set) var fullName: String!

到 :

private(set) var fullName: NSMutableAttributedString!

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
}

to:

 func boldFullName(_ fullname: String) -> NSMutableAttributedString {
let range = NSMakeRange(0, getFullName().count)
let nonBoldFontAttribute = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10)]
let boldFontAttribute = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 30)]
let boldString = NSMutableAttributedString(string: getFullName() as String, attributes:nonBoldFontAttribute)
    boldString.addAttributes(boldFontAttribute, range: range)
return boldString
}

最后当您调用时使用 attributedText 而不是 string

 messageLbl.attributedText = ...

The main problem is you are trying to give attributedString to text property which is not gonna effect on the UILabel . You must change some part of your code like :

private(set) var fullName: String!

to :

private(set) var fullName: NSMutableAttributedString!

And

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
}

to:

 func boldFullName(_ fullname: String) -> NSMutableAttributedString {
let range = NSMakeRange(0, getFullName().count)
let nonBoldFontAttribute = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 10)]
let boldFontAttribute = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 30)]
let boldString = NSMutableAttributedString(string: getFullName() as String, attributes:nonBoldFontAttribute)
    boldString.addAttributes(boldFontAttribute, range: range)
return boldString
}

And last when you call use attributedText instead of string

 messageLbl.attributedText = ...
后知后觉 2025-01-23 12:41:54

使用此扩展,

 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)
}

我修改了我的开关案例,如下所示,

 case .internalCommunicationMessage?:
        let content = NSAttributedString(string:"new_notification.announcement.text_%@".localized)
        let gn = NSAttributedString(string: model.groupName ?? "", attributes: [.font: UIFont.sfProTextMedium(size: size),
                                                                                .kern: -0.26])
        return NSAttributedString(format: content, args: gn)

更改了configureText的返回类型

configureText(model: NewNotificationViewModel) -> NSAttributedString

,删除了boldFullName函数,并将fullName类型更改回String

,最后插入到标签中,如下所示。

messageLbl.attributedText = NewNotificationTableViewCell.configureText(model: model)

with this extension

 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)
}

I modified my switch cases as below

 case .internalCommunicationMessage?:
        let content = NSAttributedString(string:"new_notification.announcement.text_%@".localized)
        let gn = NSAttributedString(string: model.groupName ?? "", attributes: [.font: UIFont.sfProTextMedium(size: size),
                                                                                .kern: -0.26])
        return NSAttributedString(format: content, args: gn)

the changed return type of configureText

configureText(model: NewNotificationViewModel) -> NSAttributedString

deleted boldFullName function and changed fullName type back to String

and finally inserted as below to label.

messageLbl.attributedText = NewNotificationTableViewCell.configureText(model: model)
傲影 2025-01-23 12:41:53

字符串不包含属性,您确实需要从函数返回一个 NSAttributedString

您可以做的是将属性字符串分配给UILabelattributedText属性。文档此处

示例(更新函数以返回 NSAttributedString):

messageLbl.attributedText = NewNotificationTableViewCell.configureText(model: model)

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 your UILabel. Documentation here

Example (after updating your function to return a NSAttributedString):

messageLbl.attributedText = NewNotificationTableViewCell.configureText(model: model)
输什么也不输骨气 2025-01-23 12:41:53

您需要分配 messageLbl.attributedText

func boldFullName(_ fullname: String) -> NSAttributedString {
    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
}

You need to assign messageLbl.attributedText

func boldFullName(_ fullname: String) -> NSAttributedString {
    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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文