小编mic*_*nez的帖子

在 UILabel 的特定字符后面绘制圆圈

我试图在 UILabel 字符的特定部分后面绘制圆圈,而不必使用多个标签或重新计算框架。

我知道我可以使用属性文本将图像插入到 UILabel 中,但我希望圆圈位于数字后面(从 1 到 9)。我不想存储从 1 到 9 的图像。我也找不到任何 pod 可以实现此目的。

    let fullText = NSMutableAttributedString()

    let imageAttachment = NSTextAttachment()
    imageAttachment.image = UIImage(named: "\(number)_\(color == .yellow ? "yellow" : "gray")")
    let imageString = NSAttributedString(attachment: imageAttachment)
    let endString = NSAttributedString("nouveaux")

    fullString.append(imageString)
    fullString.append(endString)

    mainLabel.attributedText = fullString
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑

我想在字符串的每个数字后面画一个圆圈。如果可能的话,我希望它成为一个通用工具,如果可能的话,它是一种返回属性字符串的解析器,如果没有的话,它是一个唯一的 UIView。另一个用例是“4 条消息 | 聊天 2”(4 条是黄色,2 条是灰色)

uilabel ios swift

3
推荐指数
1
解决办法
1133
查看次数

标签 统计

ios ×1

swift ×1

uilabel ×1