Beispiel #1
0
        public static int GetStringAvailableIndex(MeasureFontRequest req)
        {
            if (string.IsNullOrEmpty(req.Input))
            {
                return(-1);
            }
            var cloneReq      = req.Clone();
            var availableSize = req.AvailableSize.Width * req.AvailableSize.Height;
            var maxCharNum    = req.Input.Length;
            //判断宽高是否为一个可读数
            int availableSizeInt;

            if (Int32.TryParse(availableSize.ToString(), out availableSizeInt))
            {
                //一个字符的宽高
                cloneReq.Input         = "a";
                cloneReq.AvailableSize = new Size(Double.PositiveInfinity, Double.PositiveInfinity);
                var charSize = MeasureStringSize(cloneReq);
                maxCharNum = (int)(availableSize / charSize.Width / charSize.Height + req.AvailableSize.Width / charSize.Width);
            }
            if (req.IsNext)
            {
                cloneReq.Input = maxCharNum >= req.Input.Length ? req.Input : req.Input.Substring(0, maxCharNum);
            }
            else
            {
                cloneReq.Input = maxCharNum >= req.Input.Length ? req.Input : req.Input.Substring(req.Input.Length - maxCharNum, maxCharNum);
            }

            cloneReq.AvailableSize = req.AvailableSize;
            var size = MeasureStringSize(cloneReq);

            if (size.Height > req.AvailableSize.Height)
            {
                if (cloneReq.Input.Length - 1 == 0)
                {
                    return(0);
                }
                if (req.IsNext)
                {
                    cloneReq.Input = cloneReq.Input.Substring(0, cloneReq.Input.Length - 1);
                }
                else
                {
                    cloneReq.Input = cloneReq.Input.Substring(1, cloneReq.Input.Length - 1);
                }
                return(GetStringAvailableIndex(cloneReq));
            }
            else
            {
                return(cloneReq.Input.Length);
            }
        }
Beispiel #2
0
        public MeasureFontRequest Clone()
        {
            var clone = new MeasureFontRequest()
            {
                Input         = Input,
                FontFamily    = FontFamily,
                FontSize      = FontSize,
                AvailableSize = AvailableSize,
                IsNext        = IsNext,
            };

            return(clone);
        }
Beispiel #3
0
        public static Size MeasureStringSize(MeasureFontRequest req)
        {
            var tb = new TextBlock();

            tb.MaxLines     = req.MaxLines;
            tb.TextWrapping = TextWrapping.Wrap;
            tb.Text         = req.Input;
            tb.FontFamily   = req.FontFamily;
            tb.FontSize     = req.FontSize;
            tb.Measure(req.AvailableSize);
            Size actualSize = new Size();

            actualSize.Width  = tb.ActualWidth;
            actualSize.Height = tb.ActualHeight;
            return(actualSize);
        }