从上个月项目中就出现了文字要设置行间距的问题。在网上搜索了好久,搜索结果全是iOS原生开发的。
做Xamarin开发的应该都知道,Xamarin的API和原生都是一一对应的。
于是慢慢试着照着OC代码转成c#的。转换结果如下:
NSMutableAttributedString attributedString = new NSMutableAttributedString (cellData.BriefDescription);
var att = new UIStringAttributes ();
att.ParagraphStyle = new NSParagraphStyle ();
att.ParagraphStyle.LineSpacing = 12.0f;
att.ParagraphStyle.MaximumLineHeight = 24.0f;
att.ParagraphStyle.MinimumLineHeight = 24.0f;
attributedString.AddAttributes(att.Dictionary, new NSRange (0,cellData.BriefDescription.Length ));
lable.AttributedText = attributedString;`
但是却出不来效果。
为了不耽误项目的进度,就把这个问题放下往下做。一直到了这个月的今天。下面说下Xamarin.iOS中给UILable设置行间距的方法:
// 行间距
UIStringAttributes strAttri = new UIStringAttributes
{
Font = UIFont.SystemFontOfSize(15f),
ForegroundColor = MvxTouchColor.GrayTwelve,
ParagraphStyle = new NSMutableParagraphStyle() { LineSpacing = 5.0f}
};
var attrText = new NSMutableAttributedString(_showText);
attrText.AddAttributes(strAttri, new NSRange(0, _showText.Length));
lable.AttributedText = attrText;
大家注意,最后一句是给UIlable设置文字的,和以前的Text不一样。
还有一点需要注意,我一开始用的不是UIlable,是UIButton(应该都知道UIButton包含了UIlable和UIimage)
但是出不来。
至于为啥等我研究好了再回来补充。
最后贴出解决问题的帖子,是在Xamarin的Developers中找到的。
https://forums.xamarin.com/discussion/8978/how-change-line-spacing-into-uilabel