繁体   English   中英

使用 Swift 仅向文本添加突出显示/背景

[英]Add highlight/background to only text using Swift

我只想在未居中对齐的 label 上的文本上突出显示或添加背景。

我已经尝试过属性字符串( https://stackoverflow.com/a/38069772/676822 )并使用正则表达式,但没有找到一个好的解决方案。 NSAttributedString 不起作用,因为我的 label 没有居中并且不包含换行符。 这只是一个需要多行的长文本。

这就是我想要完成的事情:

在此处输入图像描述

注意:这不是“传播\n设计\n思考”,而是“传播设计思维”

谢谢!

据我所知,它不可能仅仅使用属性文本来获得你想要的东西,因为使用:

let attributedText = NSMutableAttributedString(string: "Evangelizing Desing Thinking",
    attributes: [
        .font: UIFont.systemFont(ofSize: 14),
        .backgroundColor: UIColor.gray
    ]
)

将在每行的末尾添加额外的灰色背景。 我之前的回答也不好,因为它只在每个单词上添加灰色背景,而不是在空格上,而且正如@Alladinian 注意到的那样,在某些情况下范围可能是错误的。

所以这里有一个技巧,你可以用它来实现你想要的。 它使用多个标签,但可以通过将标签放在自定义视图中轻松改进。 因此,在您的viewDidLoad / CustomView function 中添加:

    // Maximum desired width for your text
    let maxLabelWidth: CGFloat = 80
    // Font you used
    let font = UIFont.systemFont(ofSize: 14)
    // Your text
    let text = "Eva ngel izing Des ing a Thin king"
    // Width of a space character
    let spaceWidth = NSString(string: " ").size(withAttributes: [NSAttributedString.Key.font: font]).width

    // Width of a row
    var currentRowWidth: CGFloat = 0
    // Content of a row
    var currentRow = ""
    // Previous label added (we keep it to add constraint betweeen labels)
    var prevLabel: UILabel?

    let subStrings = text.split(separator: " ")
    for subString in subStrings {
        let currentWord = String(subString)
        let nsCurrentWord = NSString(string: currentWord)
        // Width of the new word
        let currentWordWidth = nsCurrentWord.size(withAttributes: [NSAttributedString.Key.font: font]).width
        // Width of the row if you add a new word
        let currentWidth = currentRow.count == 0 ? currentWordWidth : currentWordWidth + spaceWidth + currentRowWidth

        if currentWidth <= maxLabelWidth { // The word can be added in the current row
            currentRowWidth = currentWidth
            currentRow += currentRow.count == 0 ? currentWord : " " + currentWord
        } else { // Its not possible to add a new word in the current row, we create a label with the current row content
            prevLabel = generateLabel(with: currentRow,
                                      font: font,
                                      prevLabel: prevLabel)
            currentRowWidth = currentWordWidth
            currentRow = currentWord
        }
    }

    // Dont forget to add the last row
    generateLabel(with: currentRow,
                  font: font,
                  prevLabel: prevLabel)

然后你必须创建generateLabel function:

@discardableResult func generateLabel(with text: String,
                                      font: UIFont,
                                      prevLabel: UILabel?) -> UILabel {
    let leftPadding: CGFloat = 50 // Left padding of the label
    let topPadding: CGFloat = 100 // Top padding of (first) label

    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(label)
    label.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: leftPadding).isActive = true
    if let prevLabel = prevLabel {
        label.topAnchor.constraint(equalTo: prevLabel.bottomAnchor).isActive = true
    } else {
        label.topAnchor.constraint(equalTo: self.view.topAnchor, constant: topPadding).isActive = true
    }
    label.font = font
    label.text = text
    label.backgroundColor = .gray

    return label
}

上一个答案:

正如 Yogesh 建议的那样,您可以使用属性字符串:

    // Init label
    let label = UILabel(frame: CGRect(x: 50, y: 50, width: 90, height: 120))
    self.view.addSubview(label)
    label.lineBreakMode = .byTruncatingTail
    label.numberOfLines = 0
    label.backgroundColor = .white

    // Create attributed text
    let text = "Evangelizing Desing Thinking"
    let attributedText = NSMutableAttributedString(string: text,
        attributes: [
            .font: UIFont.systemFont(ofSize: 14)
        ]
    )

    // Find ranges of each word
    let subStrings = text.split(separator: " ")
    let ranges = subStrings.map { (subString) -> Range<String.Index> in
        guard let range = text.range(of: subString) else {
            fatalError("something wrong with substring") // This case should not happen
        }
        return range
    }

    // Apply background color for each word
    ranges.forEach { (range) in
        let nsRange = NSRange(range, in: text)
        attributedText.addAttribute(.backgroundColor, value: UIColor.gray, range: nsRange)
    }

    // Finally set attributed text
    label.attributedText = attributedText

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM