如何解决如何为标签文本添加边框/轮廓?
我需要在应用程序中为字幕添加边框,什么是最好的方法? 我尝试使用轮廓字体(examples),但它们是透明的,我需要带有黑色边框的白色文本。
解决方法
您可以使用自定义渲染器来实现此效果。
对于iOS :
创建自定义 OutLineLabel ,该扩展名扩展了ios项目中的 UILabel 。
public class OutLineLabel:UILabel
{
public OutLineLabel(string text)
{
this.Text = text;
}
public override void DrawText(CGRect rect)
{
CGSize shadowOffset = this.ShadowOffset;
UIColor textColor = UIColor.White; //the text color
CGContext c = UIGraphics.GetCurrentContext();
c.SetLineWidth(2);
c.SetLineJoin(CGLineJoin.Round);
c.SetTextDrawingMode(CGTextDrawingMode.Stroke);
this.TextColor = UIColor.Black; //the outline border color
base.DrawText(rect);
c.SetTextDrawingMode(CGTextDrawingMode.Fill);
this.TextColor = textColor;
this.ShadowOffset = new CGSize(0,0);
base.DrawText(rect);
this.ShadowOffset = shadowOffset;
}
}
在ios项目中创建自定义渲染器 OutLineLabelRenderer
[assembly: ExportRenderer(typeof(Label),typeof(OutLineLabelRenderer))]
namespace your namespace
{
class OutLineLabelRenderer:LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
OutLineLabel outLineLabel = new OutLineLabel(Element.Text);
SetNativeControl(outLineLabel);
}
}
}
对于Android
您可以参考此link。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。