IOS 无论如何要在整个应用程序中使用 NSAttributedString。 Swift
IOS Anyway to have NSAttributedString throughout app. Swift
我有一个超级瘦的字体。在 photoshop 中,我为字体添加了描边,使它看起来更吸引人。将它传输到 iOS 时,除非我使用 NSAttributedString,否则我无法添加笔划。通过使用 NSAttributedString,我得到了我的一个 UILabel,可以准确地查看它在 photoshop 中的外观,但问题是当我的应用程序完成时,我将拥有数百个 UILabel。有没有一种方法我不必手动将每个 UILabel 连接到其各自的控制器并一个一个地设置其 attributedText。任何建议都会有所帮助。
根据@AdamPro13 的建议,您可以为您的 NSAttributedString
创建一个协议扩展:
所以,用类似的东西创建 NSAttributedStringExtension.swift
:
protocol NSAttributedStringExtension {
static func attributed(string:String, font:UIFont, color:UIColor) -> NSAttributedString
}
extension NSAttributedString : NSAttributedStringExtension {
class func attributed(string:String, font:UIFont, color:UIColor) -> NSAttributedString {
let attrs = [NSFontAttributeName:font, NSForegroundColorAttributeName:color]
return NSAttributedString(string: string, attributes: attrs)
}
}
并且您可以为不同的标签类型创建多个不同的函数。
只需在您的标签上应用(粗略的代码):
let label:UILabel
let font:UIFont
let color:UIColor
label.attributedText = NSAttributedString.attributed("test", font: font, color: color)
注意:您可以在协议扩展函数中设置字体和颜色
如果您正在使用情节提要并想正常使用 UILabel,那么您可以尝试创建一个 UILabel subclass,如下所示,并将其作为自定义 class 在您的情节提要中使用。这将有效地用您想要的属性文本替换文本,尽管您要到运行时才能看到它。
class CustomLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
let attrs = [NSFontAttributeName: UIFont.boldSystemFontOfSize(19),
NSForegroundColorAttributeName: UIColor.blueColor()]
attributedText = NSAttributedString(string: text!, attributes: attrs)
}
}
我只是在搜索,然后偶然发现了这个问题。我认为您想要 UILabel
的类别。我从我以前制作的名为 GPKit https://github.com/glennposadas/gpkit-ios 的旧 Cocoapod 库中获得了一种非常有用的方法。但是下面的代码已更新并记录在案。
import UIKit
/// Category for UILabel helper functions
extension UILabel {
/// A helper function for setting up the normal properties of the label.
/// This means that the label has no special attributes.
/// This uses the system font.
func setup(_ text: String, fontSize: CGFloat, weight: UIFont.Weight = .regular, italic: Bool = false, textColor: UIColor = .lalaDarkGray, numberOfLines: Int = 1, textAlignment: NSTextAlignment = .natural) {
self.font = italic ? UIFont.italicSystemFont(ofSize: fontSize) : UIFont.systemFont(ofSize: fontSize, weight: weight)
self.text = text
self.textColor = textColor
self.numberOfLines = numberOfLines
self.textAlignment = textAlignment
}
/**
Sets up the label with two different kinds of attributes in its attributed text.
- Author: Glenn
- Important:
- primaryString = "Total:"
- secondaryString = "123"
- This means that the function will concat the secondary string into the primary string and highlights the secondary string.
- Using the highlightedText means the highlightedText itself is in the primaryString.
- parameters:
- primaryString: the normal attributed string.
- secondaryString: the bold or highlighted string.
- highlightedText: this one is like the secondary string, if this is provided, then the secondaryString is ignored. This is to be used if the highlighted text is not to be concatinated at the end of the primaryString
*/
func setAttributedText(
primaryString: String,
textColor: UIColor,
font: UIFont,
secondaryString: String = "",
secondaryTextColor: UIColor? = nil,
secondaryFont: UIFont? = nil,
highlightedText: String? = nil,
textAlignment: NSTextAlignment = .center,
numberOfLines: Int = 1,
lineHeightMultiple: CGFloat = 1) {
var textToBeHighlighted = ""
var completeString: String!
self.numberOfLines = numberOfLines
if let highlightedText = highlightedText {
textToBeHighlighted = highlightedText
completeString = primaryString
} else {
if secondaryString.hasValidValue() {
textToBeHighlighted = secondaryString
completeString = "\(primaryString) \(secondaryString)"
} else {
completeString = primaryString
}
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment
paragraphStyle.lineHeightMultiple = lineHeightMultiple
let completeAttributedString = NSMutableAttributedString(
string: completeString, attributes: [
.font: font,
.foregroundColor: textColor,
.paragraphStyle: paragraphStyle
]
)
let secondStringAttribute: [NSAttributedString.Key: Any] = [
.font: secondaryFont ?? font,
.foregroundColor: secondaryTextColor ?? textColor,
.paragraphStyle: paragraphStyle
]
let range = (completeString as NSString).range(of: textToBeHighlighted)
completeAttributedString.addAttributes(secondStringAttribute, range: range)
self.attributedText = completeAttributedString
}
}
您可以像这样在每个标签中使用它:
internal lazy var label_Total: UILabel = {
let label = UILabel()
label.setAttributedText(
primaryString: "Total".localized(),
textColor: .gray,
font: UIFont.systemFont(ofSize: 14.0),
secondaryString: "",
secondaryTextColor: .blue,
secondaryFont: UIFont.systemFont(ofSize: 14.0, weight: customFontWeight)
)
return label
}()
我有一个超级瘦的字体。在 photoshop 中,我为字体添加了描边,使它看起来更吸引人。将它传输到 iOS 时,除非我使用 NSAttributedString,否则我无法添加笔划。通过使用 NSAttributedString,我得到了我的一个 UILabel,可以准确地查看它在 photoshop 中的外观,但问题是当我的应用程序完成时,我将拥有数百个 UILabel。有没有一种方法我不必手动将每个 UILabel 连接到其各自的控制器并一个一个地设置其 attributedText。任何建议都会有所帮助。
根据@AdamPro13 的建议,您可以为您的 NSAttributedString
创建一个协议扩展:
所以,用类似的东西创建 NSAttributedStringExtension.swift
:
protocol NSAttributedStringExtension {
static func attributed(string:String, font:UIFont, color:UIColor) -> NSAttributedString
}
extension NSAttributedString : NSAttributedStringExtension {
class func attributed(string:String, font:UIFont, color:UIColor) -> NSAttributedString {
let attrs = [NSFontAttributeName:font, NSForegroundColorAttributeName:color]
return NSAttributedString(string: string, attributes: attrs)
}
}
并且您可以为不同的标签类型创建多个不同的函数。 只需在您的标签上应用(粗略的代码):
let label:UILabel
let font:UIFont
let color:UIColor
label.attributedText = NSAttributedString.attributed("test", font: font, color: color)
注意:您可以在协议扩展函数中设置字体和颜色
如果您正在使用情节提要并想正常使用 UILabel,那么您可以尝试创建一个 UILabel subclass,如下所示,并将其作为自定义 class 在您的情节提要中使用。这将有效地用您想要的属性文本替换文本,尽管您要到运行时才能看到它。
class CustomLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
let attrs = [NSFontAttributeName: UIFont.boldSystemFontOfSize(19),
NSForegroundColorAttributeName: UIColor.blueColor()]
attributedText = NSAttributedString(string: text!, attributes: attrs)
}
}
我只是在搜索,然后偶然发现了这个问题。我认为您想要 UILabel
的类别。我从我以前制作的名为 GPKit https://github.com/glennposadas/gpkit-ios 的旧 Cocoapod 库中获得了一种非常有用的方法。但是下面的代码已更新并记录在案。
import UIKit
/// Category for UILabel helper functions
extension UILabel {
/// A helper function for setting up the normal properties of the label.
/// This means that the label has no special attributes.
/// This uses the system font.
func setup(_ text: String, fontSize: CGFloat, weight: UIFont.Weight = .regular, italic: Bool = false, textColor: UIColor = .lalaDarkGray, numberOfLines: Int = 1, textAlignment: NSTextAlignment = .natural) {
self.font = italic ? UIFont.italicSystemFont(ofSize: fontSize) : UIFont.systemFont(ofSize: fontSize, weight: weight)
self.text = text
self.textColor = textColor
self.numberOfLines = numberOfLines
self.textAlignment = textAlignment
}
/**
Sets up the label with two different kinds of attributes in its attributed text.
- Author: Glenn
- Important:
- primaryString = "Total:"
- secondaryString = "123"
- This means that the function will concat the secondary string into the primary string and highlights the secondary string.
- Using the highlightedText means the highlightedText itself is in the primaryString.
- parameters:
- primaryString: the normal attributed string.
- secondaryString: the bold or highlighted string.
- highlightedText: this one is like the secondary string, if this is provided, then the secondaryString is ignored. This is to be used if the highlighted text is not to be concatinated at the end of the primaryString
*/
func setAttributedText(
primaryString: String,
textColor: UIColor,
font: UIFont,
secondaryString: String = "",
secondaryTextColor: UIColor? = nil,
secondaryFont: UIFont? = nil,
highlightedText: String? = nil,
textAlignment: NSTextAlignment = .center,
numberOfLines: Int = 1,
lineHeightMultiple: CGFloat = 1) {
var textToBeHighlighted = ""
var completeString: String!
self.numberOfLines = numberOfLines
if let highlightedText = highlightedText {
textToBeHighlighted = highlightedText
completeString = primaryString
} else {
if secondaryString.hasValidValue() {
textToBeHighlighted = secondaryString
completeString = "\(primaryString) \(secondaryString)"
} else {
completeString = primaryString
}
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment
paragraphStyle.lineHeightMultiple = lineHeightMultiple
let completeAttributedString = NSMutableAttributedString(
string: completeString, attributes: [
.font: font,
.foregroundColor: textColor,
.paragraphStyle: paragraphStyle
]
)
let secondStringAttribute: [NSAttributedString.Key: Any] = [
.font: secondaryFont ?? font,
.foregroundColor: secondaryTextColor ?? textColor,
.paragraphStyle: paragraphStyle
]
let range = (completeString as NSString).range(of: textToBeHighlighted)
completeAttributedString.addAttributes(secondStringAttribute, range: range)
self.attributedText = completeAttributedString
}
}
您可以像这样在每个标签中使用它:
internal lazy var label_Total: UILabel = {
let label = UILabel()
label.setAttributedText(
primaryString: "Total".localized(),
textColor: .gray,
font: UIFont.systemFont(ofSize: 14.0),
secondaryString: "",
secondaryTextColor: .blue,
secondaryFont: UIFont.systemFont(ofSize: 14.0, weight: customFontWeight)
)
return label
}()