博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone 文本表情显示控件
阅读量:5042 次
发布时间:2019-06-12

本文共 7457 字,大约阅读时间需要 24 分钟。

现在要将  “我真是[哭][笑]不得"   类似格式的文本显示成表情图片和文字的混排。像新浪微博或qq。

在wp的列表中。 那么其实主要工作就是解析字符串中[xx]这种格式的字符。然后以图片显示出来。

1.解析表情字符串

解析表情字符串。主要就是括号匹配+匹配出来的[xx]是不是我们的表情。

主要思路 建立2个字节数组,一个是储存可能是表情的字符串,另一个是储存不是表情的普通文本

 遍历这个表情字符串。如果遇到”[“就存到表情数组。如果遇到”]“就来验证夹在括号中的文本

是不是我们需要的表情。

2.建立一个内容列表

建立一个内容列表,这个列表里将上个步骤区分开的文本内容和表情内容装在内容列表中。用来后

续显示在控件中

3.在控件中显示文本和表情

这里用的控件是RichTextBox。RichTextBox的Blocks中可以添加段落。而段落里面可以添加文本

和图片等。

 

这里附上代码--!

FaceTextBlock.cs

namespace Controls.HcControl{    [TemplatePart(Name = FaceTextBlockCanvasName, Type = typeof(Canvas))]    public class FaceTextBlock : Control    {        #region XXX        ///         /// ContentControl的xaml样式        ///         private static string TemplateString        {            get            {                return                "
" + "
" + "
" + "
"; } } /// /// 表情图片路径 /// private const string FacesPath = @"/Controls;component/Images/Face/{0}.png"; /// /// 表情内容数组 /// private string[] FacesStr = { "[鄙视]", "[不高兴]", "[吃惊]", "[汗]", "[呵呵]" + "[挤眼]", "[囧]", "[可爱]", "[可怜]", "[困]" + "[泪]", "[怒]", "[衰]", "[嘻嘻]", "[右哼哼]", "[左哼哼]"}; #endregion #region Constants private const string FaceTextBlockCanvasName = "FaceTextBlockCanvas"; #endregion #region Member /// /// 表情和文本的画布 /// private RichTextBox _richTextBox; /// /// 文本表情内容容器 /// private List
_contentList = new List
(); #endregion #region LiseCycle public FaceTextBlock() { Template = (ControlTemplate)XamlReader.Load(TemplateString); this.Loaded += new RoutedEventHandler(FaceTextBlock_Loaded); } public override void OnApplyTemplate() { base.OnApplyTemplate(); _richTextBox = GetTemplateChild(FaceTextBlockCanvasName) as RichTextBox; } void FaceTextBlock_Loaded(object sender, RoutedEventArgs e) { OnTextChanged(); } #endregion #region Methods ///
/// 绘制文本头像啥的。 /// private void OnTextChanged() { _contentList.Clear(); //解析表情字符串 AnalyzeText(); //绘制UI DrawTextUI(); } ///
/// 解析表情字符串 /// private void AnalyzeText() { //表情栈 List
faceStack = new List
(); //文本栈 List
textStack = new List
(); //是否进入表情栈标志 bool isPushFace = false; if (!String.IsNullOrWhiteSpace(Text)) { Char[] chars = Text.ToCharArray(); for (int i = 0; i < chars.Length; i++) { #region 入栈选择判断 if (chars[i] == '[') { isPushFace = true; } if (chars[i] == ']') { isPushFace = false; } #endregion #region 字符入栈规则 if (isPushFace) { if (faceStack.Count >= 1 && faceStack.First() == '[' && chars[i] == '[') { textStack.AddRange(faceStack); faceStack.Clear(); } if (faceStack.Count <= 0 || faceStack.First() == '[') { faceStack.Add(chars[i]); } } else { if (faceStack.Count >= 2 && faceStack.First() == '[' && chars[i] == ']') { faceStack.Add(chars[i]); } else { textStack.Add(chars[i]); } } #endregion #region 字符填充内容 if (faceStack.Count > 2 && faceStack.Last() == ']') { StringBuilder faceStr = new StringBuilder(); StringBuilder textStr = new StringBuilder(); faceStr.Append(faceStack.ToArray()); textStr.Append(textStack.ToArray()); if (FacesStr.Contains(faceStr.ToString())) { //_contentList.Add(new HcTextTBContent(textStr.ToString(), this)); foreach (var item in textStack) { _contentList.Add(new HcTextTBContent(item.ToString(), this)); } _contentList.Add(new HcFaceTBContent(faceStr.ToString(), this)); } else { //textStr.Append(faceStr); //_contentList.Add(new HcTextTBContent(textStr.ToString(), this)); textStack.AddRange(faceStack); foreach (var item in textStack) { _contentList.Add(new HcTextTBContent(item.ToString(), this)); } } faceStack.Clear(); textStack.Clear(); } else { if (chars.Count() == i + 1) { //StringBuilder textStr = new StringBuilder(); //textStr.Append(textStack.ToArray()); //_contentList.Add(new HcTextTBContent(textStr.ToString(), this)); foreach (var item in textStack) { _contentList.Add(new HcTextTBContent(item.ToString(), this)); } } } #endregion } } } ///
/// 绘制表情和文本 /// private void DrawTextUI() { Paragraph paragraph = new Paragraph(); foreach (var item in _contentList) { paragraph.Inlines.Add(item.CreateContent()); } _richTextBox.Blocks.Add(paragraph); } #endregion #region Title DependencyProperty public string Text { get { return (string)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Text", typeof(string), typeof(FaceTextBlock), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTextChanged))); private static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { FaceTextBlock block = (FaceTextBlock)obj; if (block._richTextBox != null) { block.OnTextChanged(); } } #endregion }}

 

HcBaseTBContent.cs

namespace Controls.FaceTextBlock{    public class HcBaseTBContent    {        public string Text        {            get;            set;        }        public Control Parent        {            get;            set;        }        public HcBaseTBContent(string text,Control parent)        {            Text = text;            Parent = parent;        }        public virtual Inline CreateContent()        {            return null;        }    }}

HcFaceTBContent.cs

namespace Controls.FaceTextBlock{    public class HcFaceTBContent : HcBaseTBContent    {        public HcFaceTBContent(string text,Control parent)            : base(text, parent)        {                    }        public override Inline CreateContent()        {            Grid grid = new Grid();            grid.Height = Parent.FontSize + 8;            grid.Width = Parent.FontSize + 8;            Image image = new Image();            image.Margin = new Thickness(0,0,0,-9);            image.VerticalAlignment = VerticalAlignment.Bottom;            image.Height = Parent.FontSize + 8;            image.Width = Parent.FontSize + 8;            Uri uri = new Uri(string.Format(@"/Controls;component/Images/Face/{0}.png", Text), UriKind.RelativeOrAbsolute);            image.Source = new BitmapImage(uri);            InlineUIContainer iuc = new InlineUIContainer();            grid.Children.Add(image);            iuc.Child = grid;            return iuc;        }    }}

 

HcTextTBContent.cs

namespace Controls.FaceTextBlock{    public class HcTextTBContent : HcBaseTBContent    {        public HcTextTBContent(string text, Control parent)            : base(text, parent)        {        }        public override Inline CreateContent()        {            Run r = new Run() { Text = Text };            r.FontFamily = Parent.FontFamily;            r.FontSize = Parent.FontSize;            r.FontStretch = Parent.FontStretch;            r.FontStyle = Parent.FontStyle;            r.FontWeight = Parent.FontWeight;            r.Foreground = Parent.Foreground;            return r;        }    }}

 

转载于:https://www.cnblogs.com/CCSSPP/archive/2013/02/20/2919093.html

你可能感兴趣的文章
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>
Confluence 6 SQL Server 数据库驱动修改
查看>>
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>