gpt4 book ai didi

IOS无论如何在整个应用程序中都有NSAttributedString。 swift

转载 作者:行者123 更新时间:2023-11-30 13:29:16 25 4
gpt4 key购买 nike

我有一个超瘦的字体。在 Photoshop 中,我在字体中添加了描边,使其看起来更有吸引力。将其传输到 iOS 时,除非使用 NSAttributedString,否则无法添加笔划。通过使用 NSAttributedString,我得到了一个 UILabel,使其看起来与 Photoshop 中的外观一模一样,但问题是,当我的应用程序完成时,我将拥有数百个 UILabels。有没有一种方法可以让我不必手动将每个 UILabel 连接到其各自的 Controller 并一一设置其 attributeText 。任何建议都会有帮助。

最佳答案

我只是在搜索,然后偶然发现了这个问题。我认为您需要 UILabel 的类别。我的旧 Cocoapod 库中有一个非常有用的方法,名为 GPKit https://github.com/glennposadas/gpkit-ios我之前做的。但下面的代码已更新并记录。

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
}()

关于IOS无论如何在整个应用程序中都有NSAttributedString。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com