在UILabel上,如何确定是否设置了text或attributedText?

问题描述 投票:0回答:1

我具有执行以下操作的功能:

if let attributedText = attributedText {
    size = app_size(for: attributedText, withConstrainedWidth: constrainedWidth)
} else if let text = text {
    size = app_size(for: text, withConstrainedWidth: constrainedWidth)
} 

app_size各自的功能返回CGSize

尽管在操场上玩这个游戏,但我意识到无论是在text上设置attributedText还是UILabelattributedText值始终存在:

let l = UILabel()
l.text = "this is a really long label that should wrap around and stuff. it should maybe wrap 2 or three times i dunno"

print(l.text)
print(l.attributedText)

// prints:

Optional("this is a really long label that should wrap around and stuff. it should maybe wrap 2 or three times i dunno")
Optional(this is a really long label that should wrap around and stuff. it should maybe wrap 2 or three times i dunno{
    NSColor = "<UIDynamicSystemColor: 0x600003ead820; name = labelColor>";
    NSFont = "<UICTFont: 0x7fa8847014b0> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 1";
    NSShadow = "NSShadow {0, -1} color = {(null)}";
})

let l = UILabel()
l.attributedText = NSAttributedString(string: "this is a really long label that should wrap around and stuff. it should maybe wrap 2 or three times i dunno")

print(l.text)
print(l.attributedText)

// prints

Optional("this is a really long label that should wrap around and stuff. it should maybe wrap 2 or three times i dunno")
Optional(this is a really long label that should wrap around and stuff. it should maybe wrap 2 or three times i dunno{
})

所以如何在UILabel上正确检测是否设置了textattributedText?谢谢

ios swift uikit uilabel
1个回答
0
投票

我找不到任何公共API来回答您的问题。

解决方案:

创建UILabel的子类并覆盖两个方法的文本方法。

class UILabelCustom: UILabel {
    enum TextOrAttributedText {
        case text, attributedText
    }

    private (set) var textOrAttributedText: TextOrAttributedText?

    var isAttributedText: Bool {
        return textOrAttributedText == .attributedText ? true : false
    }

    var isText: Bool {
        return textOrAttributedText == .text ? true : false
    }

    override var text: String? {
        didSet {
            if text != nil {
                textOrAttributedText = .text
            } else {
                textOrAttributedText = nil
            }
        }
    }

    override var attributedText: NSAttributedString? {
        didSet {
            if attributedText != nil {
                textOrAttributedText = .attributedText
            } else {
                textOrAttributedText = nil
            }
        }
    }
}

单元测试

func testExample() {
    let l = UILabelCustom()
    let str1 = "str1"
    l.attributedText = NSAttributedString(string: str1)
    XCTAssertEqual(l.text, str1)
    XCTAssertEqual(l.attributedText?.string, str1)
    XCTAssertTrue(l.isAttributedText)
    XCTAssertFalse(l.isText)
    XCTAssertEqual(l.textOrAttributedText, .attributedText)
    XCTAssertNotEqual(l.textOrAttributedText, .text)

    let str2 = "str2"
    l.text = str2
    XCTAssertEqual(l.text, str2)
    XCTAssertEqual(l.attributedText?.string, str2)
    XCTAssertTrue(l.isText)
    XCTAssertFalse(l.isAttributedText)
    XCTAssertEqual(l.textOrAttributedText, .text)
    XCTAssertNotEqual(l.textOrAttributedText, .attributedText)
}
© www.soinside.com 2019 - 2024. All rights reserved.