简体   繁体   English

如何绑定NSAttributeString(或NSMutableAttributedString)

[英]How to bind NSAttributeString (or NSMutableAttributedString)

I want to bind an attributed string to a UILabel using MVVMCross. 我想使用MVVMCross将属性字符串绑定到UILabel。 To bind a regular string I would just do: 要绑定一个常规字符串,我只会这样做:

set.Bind(MyLabel).To(vm => vm.MyString);

But I need a string where part of the text will use one color and one font size and another part will use a different color and a different font size. 但是我需要一个字符串,其中文本的一部分将使用一种颜色和一种字体大小,而另一部分将使用另一种颜色和字体大小。 If this was static, no problem, I'd add a label in interface builder and set it "attributed" and then set whatever font options I want on which ever parts of the string I need. 如果这是静态的,没问题,我将在接口构建器中添加一个标签并将其设置为“归因”,然后在所需的字符串的任何部分上设置所需的字体选项。

So I thought with Mvvmcross, I'd probably need a converter to turn my source string into an attributed string, so I tried creating a converter from MvxValueConverter<string,NSMutableAttributedString> that just does this in its Convert method: 因此,我认为使用Mvvmcross可能需要一个转换器,以将源字符串转换为属性字符串,因此我尝试从MvxValueConverter<string,NSMutableAttributedString>创建一个转换器,只需在其Convert方法中执行此操作:

return new NSMutableAttributedString(value);

Eventually I'll actually add some different attributes. 最终,我实际上将添加一些不同的属性。 Unfortunately, this doesn't work. 不幸的是,这不起作用。 If I set my binding like this: 如果我这样设置绑定:

set.Bind(MyLabel).To(vm => vm.MyString).WithConversion("MyConverter");

It appears that MvvmCross just does a .ToString on the attributed string and it displays as: 看来MvvmCross只是在属性字符串上执行.ToString ,它显示为:

Some Text {}

Note the {} aren't part of the original string. 注意{}不是原始字符串的一部分。

Is there a way to bind an attributed string in MVVMCross? 有没有办法在MVVMCross中绑定属性字符串?

If you call 如果你打电话

 set.Bind(MyLabel).To(vm => vm.MyString);

then you are binding the default property of your UILabel which is the string property Text. 那么您将绑定UILabel的默认属性,即string属性Text。

You need to bind the AttributedText instead. 您需要绑定AttributedText。 Try adding something like: 尝试添加如下内容:

    .For(l => l.AttributedText)

There are some questions on here about using AttributedText - eg Underline text in UILabel in monotouch (porting ObjC code) 这里有一些关于使用AttributedText的问题-例如,UILabel中的下划线文本在monotouch中(移植ObjC代码)

For more on mvx data-binding see https://github.com/slodge/MvvmCross/wiki/Databinding 有关mvx数据绑定的更多信息,请参见https://github.com/slodge/MvvmCross/wiki/Databinding

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

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