如何绑定NSAttributeString(或NSMutableAttributedString) [英] How to bind NSAttributeString (or NSMutableAttributedString)

查看:71
本文介绍了如何绑定NSAttributeString(或NSMutableAttributedString)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MVVMCross将属性字符串绑定到UILabel.要绑定常规字符串,我只需要这样做:

I want to bind an attributed string to a UILabel using MVVMCross. 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.

所以我以为Mvvmcross可能需要一个转换器才能将源字符串转换为属性字符串,因此我尝试从MvxValueConverter<string,NSMutableAttributedString>创建一个转换器,只需在其Convert方法中执行此操作即可:

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:

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");

MvvmCross似乎仅对属性字符串执行.ToString,并显示为:

It appears that MvvmCross just does a .ToString on the attributed string and it displays as:

Some Text {}

请注意,{}不是原始字符串的一部分.

Note the {} aren't part of the original string.

是否可以在MVVMCross中绑定属性字符串?

Is there a way to bind an attributed string in MVVMCross?

推荐答案

如果您致电

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

然后绑定UILabel的默认属性,即string属性Text.

then you are binding the default property of your UILabel which is the string property Text.

您需要绑定AttributedText.尝试添加类似的内容:

You need to bind the AttributedText instead. Try adding something like:

    .For(l => l.AttributedText)

关于使用AttributedText,这里有一些问题-例如在UILabel中以单点触控在文本上加下划线(移植ObjC代码)

There are some questions on here about using AttributedText - eg Underline text in UILabel in monotouch (porting ObjC code)

有关mvx数据绑定的更多信息,请参见> https://github.com/slodge/MvvmCross /wiki/Databinding

For more on mvx data-binding see https://github.com/slodge/MvvmCross/wiki/Databinding

这篇关于如何绑定NSAttributeString(或NSMutableAttributedString)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆