Ios 如何在swift中设置属性字符串的格式?
我正在处理一个通知页面,其中输入大部分来自JSON文件,我需要将它们与本地化字符串结合起来。它应该是这样的: 可以预测,彩色部分来自JSON文件,其余部分来自Localizable.strings。这是来自可本地化文件的内容:Ios 如何在swift中设置属性字符串的格式?,ios,iphone,swift,string,nsattributedstring,Ios,Iphone,Swift,String,Nsattributedstring,我正在处理一个通知页面,其中输入大部分来自JSON文件,我需要将它们与本地化字符串结合起来。它应该是这样的: 可以预测,彩色部分来自JSON文件,其余部分来自Localizable.strings。这是来自可本地化文件的内容: "%@ has joined %@" 如果我使用String(格式:String,[…])我有一个纯黑色文本,我无法指定需要着色的部分 我需要为NSAttributedString提供相同的功能,但它没有此方法 那么如何设置属性字符串的格式呢?检查以下示例: var
"%@ has joined %@"
如果我使用String(格式:String,[…])
我有一个纯黑色文本,我无法指定需要着色的部分
我需要为NSAttributedString
提供相同的功能,但它没有此方法
那么如何设置属性字符串的格式呢?检查以下示例:
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: "Your full label textString")
myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: CGFloat(17.0))!
, NSForegroundColorAttributeName : UIColor(red: 232 / 255.0, green: 117 / 255.0, blue: 40 / 255.0, alpha: 1.0)], range: NSRange(location:12,length:8)) // What ever range you want to give
yourLabel.attributedText = myMutableString
或另一种方式:
要更改文本长度的颜色,您需要知道字符串中有颜色的字符的开始和结束索引,例如
var main_string = "Hello World"
var string_to_color = "World"
var range = (main_string as NSString).rangeOfString(string_to_color)
然后将其转换为属性字符串,并将“添加属性”与NSForegroundColorAttributeName一起使用:
var attributedString = NSMutableAttributedString(string:main_string)
attributedString.addAttribute(NSForegroundColorAttributeName, value: NSColor.redColor() , range: range)
尝试下面的代码并相应地更新您的逻辑
let localizableStr = "%@ has joined %@"
let localisedStr = NSLocalizedString(localizableStr, comment: "")
let components = localizableStr.components(separatedBy: "%@")
let formatterStr = components.count > 2 ? components[1] : "has joined"
let evaluatedStr = NSString(format: localisedStr as NSString, "Rishi ", "Stack OVerflow")
let attributedStr = NSMutableAttributedString(string: evaluatedStr as String)
attributedStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.brown, range: NSMakeRange(0, attributedStr.length))
let formatterStrRange = evaluatedStr.range(of: formatterStr, options: .caseInsensitive)
attributedStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: formatterStrRange)
我的两个本地化字符串:
"welcome message" = "%@ has joined %@";
"welcome message" = "انضم %@ إلى %@";
结果
如何使用
//General attr: Applied to the entire string
let generalAttributes = [NSAttributedString.Key.font: UIFont.getFont(.regular, size: 20)]
//Additional attrs applied to the replacement / dynamic bits. You can pass nil too
let nameAttributes = [ NSAttributedString.Key.backgroundColor: UIColor.red]
let companyAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.blue]
myLabel.attributedText = "welcome message".localisedAttributedString("adam".localized, "space".localized, attributes: generalAttributes, replacementAttributes: [nameAttributes, companyAttributes] )
这取决于您要应用哪种效果,但最简单的方法是在本地化值上使用HTML标记。@Larme我要更改颜色并将格式化部分加粗,请查看示例屏幕截图。UILabel是否自动应用HTML标记?我检查了示例,但您可能需要更多效果。您可以从HTML文本中初始化
NSAttributedString
(所有标记都不翻译,但只管理粗体/颜色):因此添加
和其他标记应该可以做到这一点。rangeOfString()
只返回第一个范围。如果占位符的两个值(在“%@已加入%@”
)相同,则该占位符将不起作用。rangeOfString已重命名为range(of:String),因为Swift 3如果使用“$1,$2”放置项目(并非所有语言都保持相同的顺序),也会出现问题。这就是为什么在我的项目中我使用了HTML标记。但这比什么都重要。你的解决方案可以奏效。
//General attr: Applied to the entire string
let generalAttributes = [NSAttributedString.Key.font: UIFont.getFont(.regular, size: 20)]
//Additional attrs applied to the replacement / dynamic bits. You can pass nil too
let nameAttributes = [ NSAttributedString.Key.backgroundColor: UIColor.red]
let companyAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.blue]
myLabel.attributedText = "welcome message".localisedAttributedString("adam".localized, "space".localized, attributes: generalAttributes, replacementAttributes: [nameAttributes, companyAttributes] )