作者:钓鱼翁玩围脖 | 来源:互联网 | 2023-05-22 10:31
我有这样的字符串
var str = "@text1 this is good @text1"
现在换成text1
另一个字符串,比方说t 1
.我能够替换文本,但我无法加粗.我想加粗新字符串t 1
,以便最终输出为:
@t 1这是好的@t 1
我该怎么做?
我看到的所有示例都在Objective-C中,但我想在Swift中进行.
提前致谢.
1> Prajeet Shre..:
这是在单个标签中组合粗体和普通文本的简洁方法.
延期:
Swift 3.0
extension NSMutableAttributedString {
@discardableResult func bold(_ text:String) -> NSMutableAttributedString {
let attrs:[String:AnyObject] = [NSFontAttributeName: UIFont(name: "AvenirNext-Medium", size: 12)!]
let boldString = NSMutableAttributedString(string: text, attributes:attrs)
self.append(boldString)
return self
}
@discardableResult func normal(_ text:String)->NSMutableAttributedString {
let normal = NSAttributedString(string: text)
self.append(normal)
return self
}
}
斯威夫特4
extension NSMutableAttributedString {
@discardableResult func bold(_ text: String) -> NSMutableAttributedString {
let attrs: [NSAttributedStringKey: Any] = [.font: UIFont(name: "AvenirNext-Medium", size: 12)!]
let boldString = NSMutableAttributedString(string:text, attributes: attrs)
append(boldString)
return self
}
@discardableResult func normal(_ text: String) -> NSMutableAttributedString {
let normal = NSAttributedString(string: text)
append(normal)
return self
}
}
用法:
let formattedString = NSMutableAttributedString()
formattedString
.bold("Bold Text")
.normal(" Normal Text ")
.bold("Bold Text")
let lbl = UILabel()
lbl.attributedText = formattedString
结果:
粗体文本普通文本粗体文本
2> Dejan Skleda..:
var normalText = "Hi am normal"
var boldText = "And I am BOLD!"
var attributedString = NSMutableAttributedString(string:normalText)
var attrs = [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]
var boldString = NSMutableAttributedString(string: boldText, attributes:attrs)
attributedString.append(boldString)
如果要将其分配给标签:
yourLabel.attributedText = attributedString
3> Leo Dabus..:
编辑/更新:Xcode 8.3.2•Swift 3.1
如果您了解HTML和CSS,则可以使用它轻松控制属性字符串的字体样式,颜色和大小,如下所示:
extension String {
var html2AttStr: NSAttributedString? {
return try? NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
}
}
"Red, Green and Blue".html2AttStr
4> Ian..:
如果您正在使用本地化字符串,则可能无法依赖始终位于句子末尾的粗体字符串.如果是这种情况,那么以下效果很好:
例如,查询"blah"与任何项目都不匹配
/* Create the search query part of the text, e.g. "blah".
The variable 'text' is just the value entered by the user. */
let searchQuery = "\"\(text)\""
/* Put the search text into the message */
let message = "Query \(searchQuery). does not match any items"
/* Find the position of the search string. Cast to NSString as we want
range to be of type NSRange, not Swift's Range */
let range = (message as NSString).rangeOfString(searchQuery)
/* Make the text at the given range bold. Rather than hard-coding a text size,
Use the text size configured in Interface Builder. */
let attributedString = NSMutableAttributedString(string: message)
attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(label.font.pointSize), range: range)
/* Put the text in a label */
label.attributedText = attributedString
经过数小时的搜索,这是找到解决我的问题的唯一答案。+1
5> David West..:
这是我提出的最佳方式.添加一个可以从任何地方调用的函数,并将其添加到一个没有像Constants.swift这样的类的文件中,然后你可以通过调用一行代码在很多情况下在任何字符串中加入单词:
要进入constants.swift文件:
import Foundation
import UIKit
func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
let nOnBoldFontAttribute= [NSFontAttributeName:font!]
let boldFOntAttribute= [NSFontAttributeName:boldFont!]
let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
boldString.addAttributes(boldFontAttribute, range: fullString.rangeOfString(boldPartOfString as String))
return boldString
}
然后你可以为任何UILabel调用这一行代码:
self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldSearchFont!)
//Mark: Albeit that you've had to define these somewhere:
let normalFOnt= UIFont(name: "INSERT FONT NAME", size: 15)
let boldFOnt= UIFont(name: "INSERT BOLD FONT", size: 15)
6> Jeremy Bader..:
我扩展了David West的好答案,以便您可以输入一个字符串并告诉它您想要鼓励的所有子字符串:
func addBoldText(fullString: NSString, boldPartsOfString: Array, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
let nOnBoldFontAttribute= [NSFontAttributeName:font!]
let boldFOntAttribute= [NSFontAttributeName:boldFont!]
let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)
for i in 0 ..
然后像这样调用它:
let normalFOnt= UIFont(name: "Dosis-Medium", size: 18)
let boldSearchFOnt= UIFont(name: "Dosis-Bold", size: 18)
self.UILabel.attributedText = addBoldText("Check again in 30 days to find more friends", boldPartsOfString: ["Check", "30 days", "find", "friends"], font: normalFont!, boldFont: boldSearchFont!)
这将鼓励您想要在给定字符串中加粗的所有子字符串
7> Daniel McLea..:
基于Jeremy Bader和David West的出色答案,Swift 3扩展:
extension String {
func withBoldText(boldPartsOfString: Array, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {
let nOnBoldFontAttribute= [NSFontAttributeName:font!]
let boldFOntAttribute= [NSFontAttributeName:boldFont!]
let boldString = NSMutableAttributedString(string: self as String, attributes:nonBoldFontAttribute)
for i in 0 ..
用法:
let label = UILabel()
let fOnt= UIFont(name: "AvenirNext-Italic", size: 24)!
let boldFOnt= UIFont(name: "AvenirNext-BoldItalic", size: 24)!
label.attributedText = "Make sure your face is\nbrightly and evenly lit".withBoldText(
boldPartsOfString: ["brightly", "evenly"], font: font, boldFont: boldFont)