double GetLineSpacing(Cell cell) { string text = "ABCabABC"; string str2 = "ABC\r\nAB"; Size size = MeasureHelper.MeasureText(text, cell.ActualFontFamily, cell.ActualFontSize * cell.Worksheet.ZoomFactor, cell.ActualFontStretch, cell.ActualFontStyle, cell.ActualFontWeight, new Size(double.PositiveInfinity, double.PositiveInfinity), cell.ActualWordWrap, null, false, (double)cell.Worksheet.ZoomFactor); return(MeasureHelper.MeasureText(str2, cell.ActualFontFamily, cell.ActualFontSize * cell.Worksheet.ZoomFactor, cell.ActualFontStretch, cell.ActualFontStyle, cell.ActualFontWeight, new Size(double.PositiveInfinity, double.PositiveInfinity), cell.ActualWordWrap, null, false, (double)cell.Worksheet.ZoomFactor).Height - (size.Height * 2.0)); }
double GetLineHeight(Cell cell) { TextBlock block = new TextBlock(); block.TextWrapping = TextWrapping.NoWrap; block.Text = Regex.Replace(cell.Text, @"[\n\r]", ""); return(MeasureHelper.MeasureText(block.Text, cell.ActualFontFamily, cell.ActualFontSize * cell.Worksheet.ZoomFactor, cell.ActualFontStretch, cell.ActualFontStyle, cell.ActualFontWeight, new Size(double.PositiveInfinity, double.PositiveInfinity), cell.ActualWordWrap, null, false, (double)cell.Worksheet.ZoomFactor).Height); }
Size GetPreferredEditorSize(Size maxSize, Size cellContentSize, HorizontalAlignment alignment, float indent) { if (!_ownPanel.Excel.CanEditOverflow || string.IsNullOrEmpty(Editor.Text)) { return(cellContentSize); } // 支持文本溢出单元格 Size realSize = MeasureHelper.MeasureText( Editor.Text, Editor.FontFamily, Editor.FontSize, Editor.FontStretch, Editor.FontStyle, Editor.FontWeight, maxSize, true, null, _ownPanel.Excel.UseLayoutRounding, _ownPanel.Excel.ZoomFactor); Size size = MeasureHelper.ConvertTextSizeToExcelCellSize(realSize, _ownPanel.Excel.ZoomFactor); // 多出字符'T'的宽度,不再测量 size.Width += Editor.FontSize; //string text = "T"; //Size size2 = CalcStringSize(new Size(2147483647.0, 2147483647.0), false, text); //size.Width += size2.Width; double width = Math.Min(maxSize.Width, cellContentSize.Width); if (((alignment == HorizontalAlignment.Left) || (alignment == HorizontalAlignment.Right)) && (width < (size.Width + indent))) { size.Width += indent; } return(new Size(Math.Max(width, size.Width), Math.Max(cellContentSize.Height, size.Height))); }
void ApplyStyle() { HorizontalAlignment horAlignment = BindingCell.ToHorizontalAlignment(); if (_tb.HorizontalAlignment != horAlignment) { _tb.HorizontalAlignment = horAlignment; } // uno绘制Right位置错误,慎用TextAlignment! //Windows.UI.Xaml.TextAlignment textAlignment; //switch (horAlignment) //{ // case HorizontalAlignment.Center: // textAlignment = Windows.UI.Xaml.TextAlignment.Center; // break; // case HorizontalAlignment.Right: // textAlignment = Windows.UI.Xaml.TextAlignment.Right; // break; // default: // textAlignment = Windows.UI.Xaml.TextAlignment.Left; // break; //} //if (_tb.TextAlignment != textAlignment) // _tb.TextAlignment = textAlignment; VerticalAlignment verAlignment; switch (BindingCell.ActualVerticalAlignment) { case CellVerticalAlignment.Top: verAlignment = VerticalAlignment.Top; break; case CellVerticalAlignment.Bottom: verAlignment = VerticalAlignment.Bottom; break; default: verAlignment = VerticalAlignment.Center; break; } if (_tb.VerticalAlignment != verAlignment) { _tb.VerticalAlignment = verAlignment; } var foreground = BindingCell.ActualForeground; if (foreground == null) { // 默认黑色 if (_tb.ReadLocalValue(TextBlock.ForegroundProperty) != DependencyProperty.UnsetValue) { _tb.ClearValue(TextBlock.ForegroundProperty); } } else if (foreground != _tb.Foreground) { _tb.Foreground = foreground; } var fontStyle = BindingCell.ActualFontStyle; if (_tb.FontStyle != fontStyle) { _tb.FontStyle = fontStyle; } var fontWeight = BindingCell.ActualFontWeight; if (_tb.FontWeight.Weight != fontWeight.Weight) { _tb.FontWeight = fontWeight; } var fontFamily = BindingCell.ActualFontFamily; if (fontFamily != null && _tb.FontFamily.Source != fontFamily.Source) { _tb.FontFamily = fontFamily; } bool wrap = BindingCell.ActualWordWrap; TextWrapping textWrap = wrap ? TextWrapping.Wrap : TextWrapping.NoWrap; if (_tb.TextWrapping != textWrap) { _tb.TextWrapping = textWrap; } double fontSize = BindingCell.ActualFontSize * ZoomFactor; double fitZoom = -1; if (!wrap && BindingCell.ActualShrinkToFit) { // 自动缩小字体适应单元格宽度 double textWidth = MeasureHelper.MeasureText( _tb.Text, _tb.FontFamily, fontSize, _tb.FontStretch, _tb.FontStyle, _tb.FontWeight, new Size(double.PositiveInfinity, double.PositiveInfinity), false, null, _tb.UseLayoutRounding, ZoomFactor).Width; double cellWidth = BindingCell.Worksheet.GetActualColumnWidth(BindingCell.Column.Index, BindingCell.ColumnSpan, BindingCell.SheetArea) * ZoomFactor; cellWidth = MeasureHelper.ConvertExcelCellSizeToTextSize(new Size(cellWidth, double.PositiveInfinity), ZoomFactor).Width; cellWidth = Math.Max((double)0.0, (double)(cellWidth - BindingCell.ActualTextIndent * ZoomFactor)); if (cellWidth < textWidth) { fitZoom = cellWidth / textWidth; } } if (fitZoom > 0) { fontSize *= fitZoom; } if (_tb.FontSize != fontSize) { _tb.FontSize = fontSize; } // TextBlock设置Padding时,若Padding左右之和大于Measure时给的Width,uno莫名报错,布局混乱,不易发现! Thickness margin = new Thickness(); var indent = BindingCell.ActualTextIndent * ZoomFactor; if (indent > 0 && _tb.HorizontalAlignment != HorizontalAlignment.Center) { if (_tb.HorizontalAlignment == HorizontalAlignment.Right) { margin.Right += indent; } else { margin.Left += indent; } } if (_tb.Margin != margin) { _tb.Margin = margin; } // 未用到 //var fontStretch = BindingCell.ActualFontStretch; //if (_tb.FontStretch != fontStretch) // _tb.FontStretch = fontStretch; if (BindingCell.ActualUnderline) { Underline underline = new Underline(); Run run = new Run(); run.Text = _tb.Text; underline.Inlines.Add(run); _tb.Inlines.Clear(); _tb.Inlines.Add(underline); _lastUnderline = true; } else if (_lastUnderline) { string str = _tb.Text; _tb.Inlines.Clear(); _tb.Text = str; } if (BindingCell.ActualStrikethrough) { foreach (UIElement element in (_tb.Parent as Panel).Children) { if (element is StrikethroughView) { StrikethroughView view = element as StrikethroughView; if (view.LineContainer != null) { foreach (var line in view.LineContainer.Children.OfType <Line>()) { line.Stroke = _tb.Foreground; } } break; } } } }