wxr
2020-12-18 5ea6aa8ea047d4d7b6137fa86c03109aeb1b67ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using Shared;
 
#if __IOS__
using UIKit;
using Foundation;
#else
using Android.Graphics;
#endif
 
namespace HDL_ON.UI
{
    /// <summary>
    /// TextButton
    /// 解决iOS 文本到边框有边距问题
    /// 支持根据文本动态计算高度
    /// </summary>
    public class TextButton : Button
    {
        public TextButton()
        {
#if __IOS__
            //重写修改文本到边框的内边距为0
            (this.uiView as MyButton).ContentEdgeInsets = new UIEdgeInsets(0, 0, 0, 0);
#endif
 
        }
 
        /// <summary>
        /// 设置需要限制的最大行数
        /// 但是超过行数的话会缺少省略号...,暂时无法解决
        /// </summary>
        public int MaxLine = 0;
        /// <summary>
        /// 动态计算高度
        /// 赋值文本后和指定宽度后,通过计算真实的需要宽度和指定宽度算出多行需要的真实高度
        /// 该方法有误差不能100%准确
        /// 最正确方法是根据 单行单个字体的高度加上行距来计算
        /// </summary>
        /// <returns></returns>
        public int GetMoreLineNeedHeight()
        {
            //计算需要的真实宽度
            int needWidth = this.GetTextWidth();
            //计算在指定宽度情况下,需要的真实行数
            int line = (needWidth / this.Width);
            //求余
            int modValue = (needWidth % this.Width);
            //余大于0,则多加一行
            if (modValue > 0)
            {
                line = line + 1;
            }
            if (line > 0)
            {
                if(MaxLine != 0)
                {
                    line = Math.Min(line, MaxLine);
                }
                return line * this.Height;
            }
            else
            {
                return this.Height;
            }
        }
 
        int _lineSpacing;
        /// <summary>
        /// 设置行间距
        /// </summary>
        public int LineSpacing
        {
            set
            {
                _lineSpacing = value;
                SetButtonLineSpacing(_lineSpacing);
            }
        }
 
 
#if __IOS__
        /// <summary>
        /// 按钮设置行距方法
        /// </summary>
        /// <param name="spacing"></param>
        void SetButtonLineSpacing(int spacing)
        {
            UIStringAttributes stringAttributes = new UIStringAttributes
            {
                Font = (this.uiView as MyButton).TitleLabel.Font,
                ForegroundColor = UIColor.Black,
                ParagraphStyle = new NSMutableParagraphStyle() { LineSpacing = spacing }
            };
            var AttributedText = new NSMutableAttributedString(Text);
            AttributedText.AddAttributes(stringAttributes, new NSRange(0, Text.Length));
            (this.uiView as MyButton).TitleLabel.AttributedText = AttributedText;
        }
 
        /// <summary>
        /// 显示当前文字需要几行
        /// </summary>
        /// <returns></returns>
        public int GetNeedLinesWithWidth()
        {
            return (this.GetTextWidth() / this.Width) * this.Height;
            //return 0;
        }
 
 
#else
 
        /// <summary>
        /// 按钮设置行距方法
        /// </summary>
        /// <param name="spacing"></param>
        void SetButtonLineSpacing(int spacing)
        {
           (this.AndroidView as Android.Widget.Button).SetLineSpacing(spacing, 1);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        int GetTextHeight()
        {
            string text = "Text";
            Rect rect = new Rect();
            (this.AndroidView as Android.Widget.Button).Paint.GetTextBounds(text, 0, text.Length, rect);
            return rect.Height();
        }
#endif
    }
 
 
 
}