我正在嘗試使用粗體全名函式使全名粗體。但顯然,它并沒有對其進行任何更改。我相信轉換為字串正在洗掉 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”
uj5u.com熱心網友回復:
String 不包含屬性,您確實需要NSAttributedString從函式中回傳 a 。
您可以做的是將屬性字串分配給. 檔案在這里attributedTextUILabel
示例(在更新您的函式以回傳 a 之后NSAttributedString):
messageLbl.attributedText = NewNotificationTableViewCell.configureText(model: model)
uj5u.com熱心網友回復:
你需要分配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
}
uj5u.com熱心網友回復:
主要問題是您試圖提供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
}
到:
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
}
最后當你呼叫 useattributedText而不是string
messageLbl.attributedText = ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/446944.html
標籤:迅速 uilabel nsattributedstring
上一篇:快速從多行字串回傳字典
下一篇:字典陣列中的可選項
