Swift 使自定义标识符之间的 UILabel 文本加粗
Swift make UILabel text bold in between custom identifier
我有一个文本为
的 UILabel
This is not bold, -bold- this is bold, -/bold- and this is another not bold, -bold- this is another bold -/bold-.
现在,我想将文本中每个 -bold-
和 -/bold-
之间的文本字体更改为粗体,这样它就会变成这样
This is not bold, this is bold, and this is another not bold, this is another bold.
我该怎么做?
您可以使用 NSMutableAttributedString
并将其设置为 UILabel
的 attributedText
属性.
let label = UILabel()
//Choose your bold font
let boldAttributes = [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(NSAttributedString(string: "Non-bold text #1", attributes: nil))
mutableAttributedString.append(NSAttributedString(string: "Bold text #1", attributes: boldAttributes))
mutableAttributedString.append(NSAttributedString(string: "Non-bold text #2", attributes: nil))
mutableAttributedString.append(NSAttributedString(string: "Bold text #2", attributes: boldAttributes))
label.attributedText = mutableAttributedString
首先,获取分隔符内的字符串。
let query = "This is not bold, -bold- this is bold, -/bold- and this is another not bold, -bold- this is another bold -/bold-"
let regex = try! NSRegularExpression(pattern: "-bold- (.*?) -/bold-", options: [])
var results = [String]()
regex.enumerateMatches(in: query, options: [], range: NSMakeRange(0, query.utf16.count)) { result, flags, stop in
if let r = result?.range(at: 1),
let range = Range(r, in: query) {
results.append(String(query[range]))
}
}
print(results)
接下来,创建如下所示的字符串扩展方法。
extension String {
func attributedString(with style: [NSAttributedString.Key: Any]? = nil,
and highlightedTextArray: [String],
with highlightedTextStyleArray: [[NSAttributedString.Key: Any]?]) -> NSAttributedString {
let formattedString = NSMutableAttributedString(string: self, attributes: style)
if highlightedTextArray.count != highlightedTextStyleArray.count {
return formattedString
}
for (highlightedText, highlightedTextStyle) in zip(highlightedTextArray, highlightedTextStyleArray) {
let highlightedTextRange: NSRange = (self as NSString).range(of: highlightedText as String)
formattedString.setAttributes(highlightedTextStyle, range: highlightedTextRange)
}
return formattedString
}
}
方法详情:
- 第一个参数:需要申请的字体样式等属性
完整的字符串。
- 第二个参数:要应用新样式的字符串数组。
- 第三个参数:要应用的新样式(在本例中为粗体)。
- returns 结果属性字符串。
最后调用如下方法
let attributedText = query.attributedString(with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .regular)],
and: results,
with: [[.font: UIFont.systemFont(ofSize: 12.0, weight: .bold)]])
希望对您有所帮助。
实施:
为 NSMutableAttributedString
编写扩展并实现粗体和普通字符串属性的方法。
extension NSMutableAttributedString {
func bold(_ value:String) -> NSMutableAttributedString {
let attributes:[NSAttributedString.Key : Any] = [.font : UIFont.boldSystemFont(ofSize: 15)]
self.append(NSAttributedString(string: value, attributes:attributes))
return self
}
func normal(_ value:String) -> NSMutableAttributedString {
let attributes:[NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: 15)]
self.append(NSAttributedString(string: value, attributes:attributes))
return self
}
}
用法:
textLabel.attributedText = NSMutableAttributedString().normal("This is not bold, ").bold("this is bold, ").normal("and this is another not bold, ").bold("this is another bold.")
结果:
我有一个文本为
的 UILabelThis is not bold, -bold- this is bold, -/bold- and this is another not bold, -bold- this is another bold -/bold-.
现在,我想将文本中每个 -bold-
和 -/bold-
之间的文本字体更改为粗体,这样它就会变成这样
This is not bold, this is bold, and this is another not bold, this is another bold.
我该怎么做?
您可以使用 NSMutableAttributedString
并将其设置为 UILabel
的 attributedText
属性.
let label = UILabel()
//Choose your bold font
let boldAttributes = [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(NSAttributedString(string: "Non-bold text #1", attributes: nil))
mutableAttributedString.append(NSAttributedString(string: "Bold text #1", attributes: boldAttributes))
mutableAttributedString.append(NSAttributedString(string: "Non-bold text #2", attributes: nil))
mutableAttributedString.append(NSAttributedString(string: "Bold text #2", attributes: boldAttributes))
label.attributedText = mutableAttributedString
首先,获取分隔符内的字符串。
let query = "This is not bold, -bold- this is bold, -/bold- and this is another not bold, -bold- this is another bold -/bold-"
let regex = try! NSRegularExpression(pattern: "-bold- (.*?) -/bold-", options: [])
var results = [String]()
regex.enumerateMatches(in: query, options: [], range: NSMakeRange(0, query.utf16.count)) { result, flags, stop in
if let r = result?.range(at: 1),
let range = Range(r, in: query) {
results.append(String(query[range]))
}
}
print(results)
接下来,创建如下所示的字符串扩展方法。
extension String {
func attributedString(with style: [NSAttributedString.Key: Any]? = nil,
and highlightedTextArray: [String],
with highlightedTextStyleArray: [[NSAttributedString.Key: Any]?]) -> NSAttributedString {
let formattedString = NSMutableAttributedString(string: self, attributes: style)
if highlightedTextArray.count != highlightedTextStyleArray.count {
return formattedString
}
for (highlightedText, highlightedTextStyle) in zip(highlightedTextArray, highlightedTextStyleArray) {
let highlightedTextRange: NSRange = (self as NSString).range(of: highlightedText as String)
formattedString.setAttributes(highlightedTextStyle, range: highlightedTextRange)
}
return formattedString
}
}
方法详情:
- 第一个参数:需要申请的字体样式等属性 完整的字符串。
- 第二个参数:要应用新样式的字符串数组。
- 第三个参数:要应用的新样式(在本例中为粗体)。
- returns 结果属性字符串。
最后调用如下方法
let attributedText = query.attributedString(with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .regular)],
and: results,
with: [[.font: UIFont.systemFont(ofSize: 12.0, weight: .bold)]])
希望对您有所帮助。
实施:
为 NSMutableAttributedString
编写扩展并实现粗体和普通字符串属性的方法。
extension NSMutableAttributedString {
func bold(_ value:String) -> NSMutableAttributedString {
let attributes:[NSAttributedString.Key : Any] = [.font : UIFont.boldSystemFont(ofSize: 15)]
self.append(NSAttributedString(string: value, attributes:attributes))
return self
}
func normal(_ value:String) -> NSMutableAttributedString {
let attributes:[NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: 15)]
self.append(NSAttributedString(string: value, attributes:attributes))
return self
}
}
用法:
textLabel.attributedText = NSMutableAttributedString().normal("This is not bold, ").bold("this is bold, ").normal("and this is another not bold, ").bold("this is another bold.")
结果: