Xamarin.iOS UILable行间距设置

从上个月项目中就出现了文字要设置行间距的问题。在网上搜索了好久,搜索结果全是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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值