using System; using Shared; #if __IOS__ using UIKit; using Foundation; #endif namespace HDL_ON.UI { /// /// TextButton /// 解决iOS 文本到边框有边距问题 /// /// public class TextButton : Button { public TextButton() { #if __IOS__ //重写修改文本到边框的内边距为0 (this.uiView as MyButton).ContentEdgeInsets = new UIEdgeInsets(0, 0, 0, 0); #endif } int _lineSpacing; /// /// 设置行间距 /// public int LineSpacing { set { _lineSpacing = value; SetButtonLineSpacing(_lineSpacing); } } #if __IOS__ /// /// 按钮设置行距方法 /// /// 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; } /// /// 显示当前文字需要几行 /// /// int GetNeedLinesWithWidth() { 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 /// /// 按钮设置行距方法 /// /// void SetButtonLineSpacing(int spacing) { (this.AndroidView as Android.Widget.Button).SetLineSpacing(spacing, 1); } #endif } }