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

标签 ios swift nsattributedstring uifont

我有一个超瘦的字体。在 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/

相关文章:

iOS Swift - 从 Firebase 中的日期范围查询值

ios - 如何手动请求 NSPhotoLibraryAddUsageDescription ?

ios - 属性标题未在 xib 文件中的 UIButton 中加载自定义字体

html - iOS:解析 HTML 并从中创建属性字符串和链接的最佳方法?

ios - NSAttributedString 高度受宽度和 numberOfLines 限制

ios - UICollectionView 分页 View 被切断 - 分页不起作用

ios - 弹出到 Root View Controller 问题

swift - 在 Swift 中实现可失败初始化器的最佳实践

ios - 从 UITableViewRowAction 触发 segue

ios - 如何让我的插页式 adMob 广告超时显示点击按钮