public float[] DrawString(string s, float x, float y, float width, float height, LineBreakMode lineBreak, TextAlignment align) { if (_lastFont == null) { return new float[] { 0, 0 } } ; float maxWidth = 0.0f; var cacheObjectKey = s + "_" + x + "_" + y + "_" + width + "_" + height + "_" + lineBreak + "_" + align; CacheObjectDrawString cacheObject = null; CacheObjectDrawStringDict.TryGetValue(cacheObjectKey, out cacheObject); if (cacheObject == null) { cacheObject = new CacheObjectDrawString(); cacheObject.DeleteTag = false; cacheObject.Key = cacheObjectKey; //s = FixupString(s); cacheObject.StringLinesiOSDict = new Dictionary <string, byte[]>(); cacheObject.StringLinesiOSDict[s] = FixupString(s); cacheObject.StringLines = new List <string>() { s }; CacheObjectDrawStringDict[cacheObjectKey] = cacheObject; } var fm = GetFontMetrics(); string sPart = ""; float stringWidth = 0.0f; if (cacheObject.LineBreak != lineBreak) { cacheObject.StringLines = new List <string>(); cacheObject.LineBreak = lineBreak; switch (lineBreak) { case LineBreakMode.WordWrap: var wordParts = s.Split(new string[] { " " }, StringSplitOptions.None); stringWidth = 0.0f; sPart = ""; for (int i = 0; i < wordParts.Length; i++) { var item = wordParts[i]; stringWidth = fm.StringWidth(sPart + item + " "); if (stringWidth > width && sPart.Length > 0) { sPart = sPart.Remove(sPart.Length - 1); //Remove space at the end cacheObject.StringLines.Add(sPart); cacheObject.StringLinesiOSDict[sPart] = FixupString(sPart); sPart = ""; } sPart += item + " "; } if (sPart.Length > 0) { sPart = sPart.Remove(sPart.Length - 1); //Remove space at the end cacheObject.StringLines.Add(sPart); cacheObject.StringLinesiOSDict[sPart] = FixupString(sPart); } break; case LineBreakMode.Wrap: //Cut the string if the width is reached var charArray = s.ToCharArray(); sPart = ""; stringWidth = 0.0f; for (int i = 0; i < charArray.Length; i++) { var item = charArray[i]; stringWidth = fm.StringWidth(sPart + item); if (stringWidth > width && sPart.Length > 0) { cacheObject.StringLines.Add(sPart); cacheObject.StringLinesiOSDict[sPart] = FixupString(sPart); sPart = ""; } sPart += item; } if (sPart.Length > 0) { cacheObject.StringLines.Add(sPart); cacheObject.StringLinesiOSDict[sPart] = FixupString(sPart); } break; default: cacheObject.StringLines.Add(s); break; } } switch (align) { case TextAlignment.Right: //y += fm.Ascent - fm.Descent; y += fm.Ascent; foreach (var item in cacheObject.StringLines) { stringWidth = fm.StringWidth(item); if (stringWidth > maxWidth) { maxWidth = stringWidth; } //_c.DrawText(item, x + width - stringWidth, y, _paints.Fill); _c.ShowTextAtPoint(x + width - stringWidth, y, cacheObject.StringLinesiOSDict[item]); y += fm.Ascent + fm.Descent; } break; case TextAlignment.Center: //y += fm.Ascent - fm.Descent; y += fm.Ascent; foreach (var item in cacheObject.StringLines) { stringWidth = fm.StringWidth(item); if (stringWidth > maxWidth) { maxWidth = stringWidth; } //_c.DrawText(item, x + width * 0.5f - stringWidth * 0.5f, y, _paints.Fill); _c.ShowTextAtPoint(x + width * 0.5f - stringWidth * 0.5f, y, FixupString(item)); y += fm.Ascent + fm.Descent; } break; default: //y += fm.Ascent - fm.Descent; y += fm.Ascent; foreach (var item in cacheObject.StringLines) { stringWidth = fm.StringWidth(item); if (stringWidth > maxWidth) { maxWidth = stringWidth; } //_c.DrawText(item, x, y, _paints.Fill); _c.ShowTextAtPoint(x, y, FixupString(item)); y += fm.Ascent + fm.Descent; } break; } return(new float[] { maxWidth, (fm.Ascent + fm.Descent) * cacheObject.StringLines.Count }); //alt: //var fm = GetFontMetrics (); //var fix = FixupString (s); //var xx = x; //var yy = y; //if (align == TextAlignment.Center) { // xx = (x + width / 2) - (fm.StringWidth (s) / 2); //} //else if (align == TextAlignment.Right) { // xx = (x + width) - fm.StringWidth (s); //} //if (fix == null) { // _c.ShowTextAtPoint(xx, yy + fm.Height, s); //} //else { // _c.ShowTextAtPoint(xx, yy + fm.Height, fix); //} ////return new double[] { (double)fm.StringWidth(s), (double)fm.Height }; //return new double[] { 0, 0 }; }
public float[] DrawString(string s, float x, float y, float width, float height, LineBreakMode lineBreak, TextAlignment align) { if (string.IsNullOrWhiteSpace(s)) { return new float[] { } } ; x *= _scaledDensity; y *= _scaledDensity; width *= _scaledDensity; height *= _scaledDensity; float maxWidth = 0.0f; var cacheObjectKey = s + "_" + x + "_" + y + "_" + width + "_" + height + "_" + lineBreak + "_" + align; CacheObjectDrawString cacheObject = null; CacheObjectDrawStringDict.TryGetValue(cacheObjectKey, out cacheObject); if (cacheObject == null) { cacheObject = new CacheObjectDrawString(); cacheObject.DeleteTag = false; cacheObject.Key = cacheObjectKey; cacheObject.StringLines = new List <string>() { s }; CacheObjectDrawStringDict[cacheObjectKey] = cacheObject; } SetFontOnPaints(); var fm = GetFontMetrics(); string sPart = ""; float stringWidth = 0.0f; if (cacheObject.LineBreak != lineBreak) { cacheObject.StringLines = new List <string>(); cacheObject.LineBreak = lineBreak; switch (lineBreak) { case LineBreakMode.WordWrap: var wordParts = s.Split(new string[] { " " }, StringSplitOptions.None); stringWidth = 0.0f; sPart = ""; for (int i = 0; i < wordParts.Length; i++) { var item = wordParts[i]; stringWidth = fm.StringWidth(sPart + item + " ") * _scaledDensity; if (stringWidth > width && sPart.Length > 0) { sPart = sPart.Remove(sPart.Length - 1); //Remove space at the end cacheObject.StringLines.Add(sPart); sPart = ""; } sPart += item + " "; } if (sPart.Length > 0) { sPart = sPart.Remove(sPart.Length - 1); //Remove space at the end cacheObject.StringLines.Add(sPart); } break; case LineBreakMode.Wrap: //Cut the string if the width is reached var charArray = s.ToCharArray(); sPart = ""; stringWidth = 0.0f; for (int i = 0; i < charArray.Length; i++) { var item = charArray[i]; stringWidth = fm.StringWidth(sPart + item) * _scaledDensity; if (stringWidth > width && sPart.Length > 0) { cacheObject.StringLines.Add(sPart); sPart = ""; } sPart += item; } if (sPart.Length > 0) { cacheObject.StringLines.Add(sPart); } break; default: cacheObject.StringLines.Add(s); break; } cacheObject.StringLines = cacheObject.StringLines; } switch (align) { case TextAlignment.Right: //y += fm.Ascent - fm.Descent; y += fm.Ascent; foreach (var item in cacheObject.StringLines) { stringWidth = fm.StringWidth(item) * _scaledDensity; if (stringWidth > maxWidth) { maxWidth = stringWidth; } _c.DrawText(item, x + width - stringWidth, y, _paints.Fill); y += fm.Ascent + fm.Descent; } break; case TextAlignment.Center: //y += fm.Ascent - fm.Descent; y += fm.Ascent; foreach (var item in cacheObject.StringLines) { stringWidth = fm.StringWidth(item) * _scaledDensity; if (stringWidth > maxWidth) { maxWidth = stringWidth; } _c.DrawText(item, x + width * 0.5f - stringWidth * 0.5f, y, _paints.Fill); y += fm.Ascent + fm.Descent; } break; default: //y += fm.Ascent - fm.Descent; y += fm.Ascent; foreach (var item in cacheObject.StringLines) { stringWidth = fm.StringWidth(item) * _scaledDensity; if (stringWidth > maxWidth) { maxWidth = stringWidth; } _c.DrawText(item, x, y, _paints.Fill); y += fm.Ascent + fm.Descent; } break; } return(new float[] { maxWidth / _scaledDensity, ((fm.Ascent + fm.Descent) * cacheObject.StringLines.Count) / _scaledDensity }); }