ReactiveX / RxSwift

Reactive Programming in Swift
MIT License
24.39k stars 4.17k forks source link

关于 UI 控件设置 rx.attributedTitle(for:) 的问题 #1657

Closed baagod closed 6 years ago

baagod commented 6 years ago
let mAtt = NSMutableAttributedString(string: "string")
let rely = BehaviorRelay(value: mAtt)

// ✅
BehaviorRelay(value: mAtt).bind(to: label.rx.attributedText)
// ✅
BehaviorRelay(value: mAtt).bind(to: button.rx.attributedTitle())

// ❌ 和 NSAttributedString 不兼容,提示:Ambiguous reference to member 'attributedText'
rely.bind(to: label.rx.attributedText)
// ❌ 和 NSAttributedString 不兼容,提示:Ambiguous reference to member 'bind(to:)'
rely.bind(to: button.rx.attributedTitle())

let att = NSAttributedString(string: "string")
let rely2 = BehaviorRelay(value: att)

// ✅
BehaviorRelay(value: att).bind(to: label.rx.attributedText)
// ✅
BehaviorRelay(value: att).bind(to: button.rx.attributedTitle())
// ✅
rely2.bind(to: label.rx.attributedText)
// ✅
rely2.bind(to: button.rx.attributedTitle())

我是想让 NSAttributedStringNSMutableAttributedString 都能兼容,否则转换麻烦,不知你能否改进下?

本来 NSMutableAttributedString 应该是兼容 NSAttributedString 的吧?这种扩展也会失败:

/// 我把返回值 NSAttributedString 改成了 NSMutableAttributedString 依旧不行,
/// 似乎只能容纳特定的值。
public var attributedText: Binder<NSMutableAttributedString?> {
    return Binder(self.base) { label, text in
        label.attributedText = text
    }
}

let att = NSAttributedString(string: "string")
let rely = BehaviorRelay(value: att)
// ❌ 还是不行。
rely.bind(to: label.rx.attributedText)

你能理解吗?英语不太好请见谅,但愿能解决我的问题。

qazyn951230 commented 6 years ago

let rely: BehaviorRelay<NSAttributedString> = BehaviorRelay(value: mAtt)

baagod commented 6 years ago

好,谢谢了。@qazyn951230

Maru-zhang commented 6 years ago

@HjzCy Maybe it's time to close your issue.