| | |
| | | #if __IOS__ |
| | | using UIKit; |
| | | using Foundation; |
| | | #else |
| | | using Android.Graphics; |
| | | #endif |
| | | |
| | | namespace HDL_ON.UI |
| | |
| | | /// <summary> |
| | | /// TextButton |
| | | /// 解决iOS 文本到边框有边距问题 |
| | | /// |
| | | /// 支持根据文本动态计算高度 |
| | | /// </summary> |
| | | public class TextButton : Button |
| | | { |
| | |
| | | (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> |
| | | /// <returns></returns> |
| | | int GetNeedLinesWithWidth() |
| | | public int GetNeedLinesWithWidth() |
| | | { |
| | | return 0; |
| | | return (this.GetTextWidth() / this.Width) * this.Height; |
| | | //return 0; |
| | | } |
| | | /** |
| | | |
| | | |
| | | // @param width 给定一个宽度 |
| | | // @return 返回行数 |
| | | // */ |
| | | //- (NSInteger) needLinesWithWidth:(CGFloat) width |
| | | // { |
| | | // //创建一个labe |
| | | // UILabel * label = [[UILabel alloc] |
| | | // init]; |
| | | // //font和当前label保持一致 |
| | | // label.font = self.font; |
| | | // NSString* text = self.text; |
| | | // NSInteger sum = 0; |
| | | // //总行数受换行符影响,所以这里计算总行数,需要用换行符分隔这段文字,然后计算每段文字的行数,相加即是总行数。 |
| | | // NSArray* splitText = [text componentsSeparatedByString: @"\n"]; |
| | | // for (NSString* sText in splitText) |
| | | // { |
| | | // label.text = sText; |
| | | // //获取这段文字一行需要的size |
| | | // CGSize textSize = [label systemLayoutSizeFittingSize: CGSizeZero]; |
| | | // //size.width/所需要的width 向上取整就是这段文字占的行数 |
| | | // NSInteger lines = ceilf(textSize.width / width); |
| | | // //当是0的时候,说明这是换行,需要按一行算。 |
| | | // lines = lines == 0 ? 1 : lines; |
| | | // sum += lines; |
| | | // } |
| | | // return sum; |
| | | // } |
| | | |
| | | //#elif __Android__ |
| | | #else |
| | | |
| | | /// <summary> |
| | |
| | | (this.AndroidView as Android.Widget.Button).SetLineSpacing(spacing, 1); |
| | | } |
| | | |
| | | #endif |
| | | /// <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 |
| | | } |
| | | |
| | | |
| | | |