本文整理汇总了C#中NSString.DrawString方法的典型用法代码示例。如果您正苦于以下问题:C# NSString.DrawString方法的具体用法?C# NSString.DrawString怎么用?C# NSString.DrawString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NSString
的用法示例。
在下文中一共展示了NSString.DrawString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImageFromFont
public static UIImage ImageFromFont(string text, UIColor iconColor, CGSize iconSize, string fontName)
{
UIGraphics.BeginImageContextWithOptions(iconSize, false, 0);
var textRect = new CGRect(CGPoint.Empty, iconSize);
var path = UIBezierPath.FromRect(textRect);
UIColor.Clear.SetFill();
path.Fill();
var font = UIFont.FromName(fontName, iconSize.Width);
using (var label = new UILabel() { Text = text, Font = font })
{
GetFontSize(label, iconSize, 500, 5);
font = label.Font;
}
iconColor.SetFill();
using (var nativeString = new NSString(text))
{
nativeString.DrawString(textRect, new UIStringAttributes
{
Font = font,
ForegroundColor = iconColor,
BackgroundColor = UIColor.Clear,
ParagraphStyle = new NSMutableParagraphStyle
{
Alignment = UITextAlignment.Center
}
});
}
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
image = image.ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal);
return image;
}
示例2: CreateImage
private UIImage CreateImage (NSString title, nfloat scale)
{
var titleAttrs = new UIStringAttributes () {
Font = UIFont.FromName ("HelveticaNeue", 13f),
ForegroundColor = Color.Gray,
};
var titleBounds = new CGRect (
new CGPoint (0, 0),
title.GetSizeUsingAttributes (titleAttrs)
);
var image = Image.TagBackground;
var imageBounds = new CGRect (
0, 0,
(float)Math.Ceiling (titleBounds.Width) + image.CapInsets.Left + image.CapInsets.Right + 4f,
(float)Math.Ceiling (titleBounds.Height) + image.CapInsets.Top + image.CapInsets.Bottom
);
titleBounds.X = image.CapInsets.Left + 2f;
titleBounds.Y = image.CapInsets.Top;
UIGraphics.BeginImageContextWithOptions (imageBounds.Size, false, scale);
try {
image.Draw (imageBounds);
title.DrawString (titleBounds, titleAttrs);
return UIGraphics.GetImageFromCurrentImageContext ();
} finally {
UIGraphics.EndImageContext ();
}
}
示例3: CreateImage
private UIImage CreateImage (NSString title, float scale)
{
var titleAttrs = new UIStringAttributes () {
Font = UIFont.FromName ("HelveticaNeue", 13f),
ForegroundColor = Color.Gray,
};
var titleBounds = title.GetBoundingRect (
new SizeF (Single.PositiveInfinity, Single.PositiveInfinity),
NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesDeviceMetrics,
titleAttrs,
null);
var image = Image.TagBackground;
var imageBounds = new RectangleF (
0, 0,
(float)Math.Ceiling (titleBounds.Width) + image.CapInsets.Left + image.CapInsets.Right + 4f,
(float)Math.Ceiling (titleBounds.Height) + image.CapInsets.Top + image.CapInsets.Bottom
);
titleBounds.X = image.CapInsets.Left + 2f;
titleBounds.Y = image.CapInsets.Top;
UIGraphics.BeginImageContextWithOptions (imageBounds.Size, false, scale);
try {
image.Draw (imageBounds);
title.DrawString (titleBounds, titleAttrs);
return UIGraphics.GetImageFromCurrentImageContext ();
} finally {
UIGraphics.EndImageContext ();
}
}
示例4: Draw
public override void Draw(RectangleF rect)
{
base.Draw (rect);
var ctx = UIGraphics.GetCurrentContext();
var center = new PointF(Bounds.X + Bounds.Width / 2.0f, Bounds.Y + Bounds.Height / 2.0f);
var maxRadius = distance(Bounds.Width, Bounds.Height) / 2.0f;
ctx.SetLineWidth(10);
CircleColor.SetStroke();
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20.0f) {
ctx.AddArc(center.X, center.Y, currentRadius, 0.0f, (float)Math.PI * 2.0f, true);
ctx.StrokePath();
}
var text = new NSString("You are getting sleepy.");
var font = UIFont.BoldSystemFontOfSize(28.0f);
var textSize = text.StringSize(font);
UIColor.Black.SetFill();
var offset = new SizeF(4.0f, 3.0f);
ctx.SetShadowWithColor(offset, 2.0f, UIColor.DarkGray.CGColor);
text.DrawString(new PointF(center.X - textSize.Width / 2.0f, center.Y - textSize.Height / 2.0f), font);
}
示例5: Draw
public override void Draw(CGRect rect)
{
if (SelectionColor == null)
SelectionColor = UIColor.Red;
UIImage img = UIImage.FromFile(FMCalendar.BasePath + "datecell.png");
UIColor color = UIColor.Black;
if (!Active || !Available)
{
color = UIColor.FromRGBA(0.576f, 0.608f, 0.647f, 1f);
if(Selected)
color = SelectionColor;
} else if (Today && Selected)
{
color = UIColor.White;
img = UIImage.FromFile(FMCalendar.BasePath + "today.png");
} else if (Today)
{
color = UIColor.White;
img = UIImage.FromFile(FMCalendar.BasePath + "today.png");
} else if (Selected)
{
color = SelectionColor;
}
img.Draw(new CGPoint(0, 0));
color.SetColor();
NSString nativeText = new NSString(Text);
nativeText.DrawString(CGRect.Inflate(Bounds, 4, -8),
UIFont.SystemFontOfSize(20),
UILineBreakMode.WordWrap, UITextAlignment.Center);
if (Marked)
{
var context = UIGraphics.GetCurrentContext();
if (Selected && !Today)
SelectionColor.SetColor ();
else if (Today)
UIColor.White.SetColor ();
else if (!Active || !Available)
UIColor.LightGray.SetColor ();
else
UIColor.Black.SetColor ();
context.SetLineWidth(0);
context.AddEllipseInRect(new CGRect(Frame.Size.Width/2 - 2, 45-10, 4, 4));
context.FillPath();
}
}
示例6: Render
static UIImage Render (string value)
{
NSString text = new NSString (string.IsNullOrEmpty (value) ? " " : value);
UIFont font = UIFont.SystemFontOfSize (20);
SizeF size = text.StringSize (font);
UIGraphics.BeginImageContextWithOptions (size, false, 0.0f);
UIColor.Red.SetColor ();
text.DrawString (new PointF (0, 0), font);
UIImage image = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return image;
}
示例7: ImageFromFont
public static UIImage ImageFromFont(UIFont font, char character, UIColor fillColor)
{
var s = new NSString("" + character);
var stringSize = s.StringSize(font);
var maxSize = (nfloat)Math.Max(stringSize.Height, stringSize.Width);
var size = new CGSize(maxSize, maxSize);
UIGraphics.BeginImageContextWithOptions(size, false, 0f);
fillColor.SetFill();
var drawPoint = new CGPoint((size.Width / 2f) - (stringSize.Width / 2f),
(size.Height / 2f) - (stringSize.Height / 2f));
s.DrawString(drawPoint, font);
var img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return img;
}
示例8: Render
static NSImage Render (string value)
{
NSImage image = null;
NSApplication.SharedApplication.InvokeOnMainThread (() =>
{
NSString text = new NSString (string.IsNullOrEmpty (value) ? " " : value);
NSFont font = NSFont.FromFontName ("Arial", 20);
var fontDictionary = NSDictionary.FromObjectsAndKeys (new NSObject[] { font, NSColor.Red }, new NSObject[] { NSStringAttributeKey.Font, NSStringAttributeKey.ForegroundColor });
CGSize size = text.StringSize (fontDictionary);
image = new NSImage (new CGSize (size));
image.LockFocus ();
text.DrawString (new CGPoint (0, 0), fontDictionary);
image.UnlockFocus ();
});
return image;
}
示例9: GetCharData
public Size GetCharData(char c,out UInt32[] cdata)
{
NSString str =new NSString(c.ToString());
var size=str.StringSize(font);
UIGraphics.BeginImageContextWithOptions(size,false,1.0f);
UIGraphics.GetCurrentContext().SetFillColor(1.0f,1.0f,1.0f,1.0f);
str.DrawString(new System.Drawing.PointF(0,0),font);
UIImage img =UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
//
int width=(int)size.Width;
int height=(int)size.Height;
cdata=new UInt32[width*height];
var gdata=new byte[width*height*4];
var colorSpace = CGColorSpace.CreateDeviceRGB();
var gbmp = new CGBitmapContext(gdata, width, height,
8, width * 4, colorSpace, CGBitmapFlags.PremultipliedLast);
//gbmp.ClearRect(new RectangleF(0,0,width,height));
gbmp.DrawImage(new System.Drawing.RectangleF(0,0,width,height),img.CGImage);
gbmp.Dispose();
colorSpace.Dispose();
unsafe
{
fixed(byte* srcb = gdata)
fixed (UInt32* dest=cdata)
{
UInt32* src=(UInt32*)srcb;
for(int y=0;y<height;y++)
{
for(int x=0;x<width;x++)
{
dest[y*width+x]=src[y*width+x];
}
}
}
}
return new Size(width,height);
}
示例10: DrawRecipeInfo
// Custom drawing code to put the recipe recipe description, and prep time
// in the title section of the recipe presentation's header.
void DrawRecipeInfo (string info, RectangleF rect)
{
RectangleF infoRect = RectangleF.Empty;
infoRect.X = rect.Left + RecipeInfoHeight;
infoRect.Y = rect.Top + TitleSize * 2;
infoRect.Width = rect.Width - RecipeInfoHeight;
infoRect.Height = RecipeInfoHeight - TitleSize * 2;
UIColor.DarkGray.SetColor ();
using (NSString str = new NSString (info)) {
str.DrawString (infoRect, SystemFont, UILineBreakMode.Clip, UITextAlignment.Left);
}
}
示例11: DrawRecipeName
// Custom drawing code to put the recipe name in the title section of the recipe presentation's header.
void DrawRecipeName (string name, RectangleF rect)
{
RectangleF nameRect = RectangleF.Empty;
nameRect.X = rect.Left + RecipeInfoHeight;
nameRect.Y = rect.Top + Padding;
nameRect.Width = rect.Width - RecipeInfoHeight;
nameRect.Height = RecipeInfoHeight;
using (UIFont font = UIFont.BoldSystemFontOfSize (TitleSize)) {
using (NSString str = new NSString (name)) {
str.DrawString (nameRect, font, UILineBreakMode.Clip, UITextAlignment.Left);
}
}
}
示例12: DrawTextWithUTF8Fonts
public void DrawTextWithUTF8Fonts (CGContext mBmpContext, string text, float x, float y, string font_family, double text_size)
{
/*
The problem is that CoreGraphics APIs for CGContext do not support rendering UTF8 code, and then ShowTextAtPoint() is limited to the text encoding in MacRoman.
The fix is to use the UIKit function DrawString() instead.
*/
UIGraphics.PushContext (mBmpContext);
mBmpContext.SetRGBFillColor(1f,1f,1f,1f);
UIFont tfont = UIFont.FromName (font_family,(float)text_size);
NSString nsstr = new NSString(text);
SizeF sz = nsstr.StringSize(tfont);
RectangleF rect = new RectangleF(x,y,sz.Width,sz.Height);
nsstr.DrawString( rect, tfont);
UIGraphics.PopContext ();
}
示例13: DrawVideoCompositionTracks
private void DrawVideoCompositionTracks (CGRect bannerRect, CGRect rowRect, ref float runningTop)
{
bannerRect.Y = runningTop;
var context = UIGraphics.GetCurrentContext ();
context.SetFillColor (1.00f, 1.00f, 1.00f, 1.00f);
var compositionTitle = new NSString ("AVComposition");
compositionTitle.DrawString (bannerRect, UIFont.PreferredCaption1);
runningTop += (float)bannerRect.Height;
rowRect.Y = runningTop;
CGRect stageRect = rowRect;
foreach (APLVideoCompositionStageInfo stage in videoCompositionStages) {
stageRect.Width = (float)stage.TimeRange.Duration.Seconds * scaledDurationToWidth;
int layerCount = stage.LayerNames.Count;
CGRect layerRect = stageRect;
if (layerCount > 0)
layerRect.Height /= layerCount;
foreach (string layerName in stage.LayerNames) {
CGRect bufferRect = layerRect;
int intValueOfName;
Int32.TryParse (layerName, out intValueOfName);
if (intValueOfName % 2 == 1) {
context.SetFillColor (0.55f, 0.02f, 0.02f, 1.00f); // darker red
context.SetStrokeColor (0.87f, 0.10f, 0.10f, 1.00f); // brighter red
} else {
context.SetFillColor (0.00f, 0.40f, 0.76f, 1.00f); // darker blue
context.SetStrokeColor (0.00f, 0.67f, 1.00f, 1.00f); // brighter blue
}
context.SetLineWidth (2f);
bufferRect = bufferRect.Inset (2f, 3f);
context.AddRect (bufferRect);
context.DrawPath (CGPathDrawingMode.FillStroke);
context.SetFillColor (0.00f, 0.00f, 0.00f, 1.00f); // white
DrawVerticallyCenteredInRect (layerName, bufferRect);
// Draw the opacity ramps for each layer as per the layerInstructions
List<CGPoint> rampArray = new List<CGPoint> ();
if (stage.OpacityRamps != null)
rampArray = stage.OpacityRamps [layerName];
if (rampArray.Count > 0) {
CGRect rampRect = bufferRect;
rampRect.Width = (float)duration.Seconds * scaledDurationToWidth;
rampRect = rampRect.Inset (3f, 3f);
context.BeginPath ();
context.SetStrokeColor (0.95f, 0.68f, 0.09f, 1.00f); // yellow
context.SetLineWidth (2f);
bool firstPoint = true;
foreach (CGPoint point in rampArray) {
CGPoint timeVolumePoint = point;
CGPoint pointInRow = new CGPoint ();
pointInRow.X = (float)HorizontalPositionForTime (CMTime.FromSeconds (timeVolumePoint.X, 1)) - 9.0f;
pointInRow.Y = rampRect.Y + (0.9f - 0.8f * timeVolumePoint.Y) * rampRect.Height;
pointInRow.X = (float)Math.Max (pointInRow.X, rampRect.GetMinX ());
pointInRow.X = (float)Math.Min (pointInRow.X, rampRect.GetMaxX ());
if (firstPoint) {
context.MoveTo (pointInRow.X, pointInRow.Y);
firstPoint = false;
} else
context.AddLineToPoint (pointInRow.X, pointInRow.Y);
}
context.StrokePath ();
}
layerRect.Y += layerRect.Height;
}
stageRect.X += stageRect.Width;
}
runningTop += (float)rowRect.Height;
runningTop += GapAfterRows;
}
示例14: DrawCompositionTracks
private void DrawCompositionTracks (CGRect bannerRect, CGRect rowRect, ref float runningTop)
{
bannerRect.Y = runningTop;
CGContext context = UIGraphics.GetCurrentContext ();
context.SetFillColor (1.00f, 1.00f, 1.00f, 1.00f);
NSString compositionTitle = new NSString ("AVComposition");
compositionTitle.DrawString (bannerRect, UIFont.PreferredCaption1);
runningTop += (float)bannerRect.Height;
foreach (List<APLCompositionTrackSegmentInfo> track in compositionTracks) {
rowRect.Y = runningTop;
CGRect segmentRect = rowRect;
foreach (APLCompositionTrackSegmentInfo segment in track) {
segmentRect.Width = (float)segment.TimeRange.Duration.Seconds * scaledDurationToWidth;
if (segment.Empty) {
context.SetFillColor (0.00f, 0.00f, 0.00f, 1.00f);
DrawVerticallyCenteredInRect ("empty", segmentRect);
} else {
if (segment.MediaType == AVMediaType.Video) {
context.SetFillColor (0.00f, 0.36f, 0.36f, 1.00f); // blue-green
context.SetStrokeColor (0.00f, 0.50f, 0.50f, 1.00f); // brigher blue-green
} else {
context.SetFillColor (0.00f, 0.24f, 0.36f, 1.00f); // bluer-green
context.SetStrokeColor (0.00f, 0.33f, 0.60f, 1.00f); // brigher bluer-green
}
context.SetLineWidth (2f);
segmentRect = segmentRect.Inset (3f, 3f);
context.AddRect (segmentRect);
context.DrawPath (CGPathDrawingMode.FillStroke);
context.SetFillColor (0.00f, 0.00f, 0.00f, 1.00f); // white
DrawVerticallyCenteredInRect (segment.Description, segmentRect);
}
segmentRect.X += segmentRect.Width;
}
runningTop += (float)rowRect.Height;
}
runningTop += GapAfterRows;
}
示例15: Draw
public override void Draw(RectangleF rect)
{
var fontSize = BadgeView.fontSize;
var text = new NSString (Text);
var numberSize = text.StringSize (UIFont.BoldSystemFontOfSize (fontSize));
var radius = Radius;
var bounds = new RectangleF (PointF.Empty, new SizeF (numberSize.Width + 12f, 18f));
UIColor color;
if (Parent.SelectionStyle != UITableViewCellSelectionStyle.None && (Parent.Highlighted || Parent.Selected)) {
color = HighlightColor;
} else {
color = Color;
}
bounds.X = (bounds.Width - numberSize.Width) / 2f + .5f;
bounds.Y += 2f;
CALayer badge = new CALayer ();
badge.Frame = rect;
var imageSize = badge.Frame.Size;
// Render the image @x2 for retina people
if (UIScreen.MainScreen.Scale == 2) {
imageSize = new SizeF (imageSize.Width * 2, imageSize.Height * 2);
badge.Frame = new RectangleF (badge.Frame.Location, new SizeF (
badge.Frame.Width * 2, badge.Frame.Height * 2));
fontSize *= 2;
bounds.X = ((bounds.Width * 2) - (numberSize.Width * 2)) / 2f + 1;
bounds.Y += 3;
bounds.Width *= 2;
radius *= 2;
}
badge.BackgroundColor = color.CGColor;
badge.CornerRadius = radius;
UIGraphics.BeginImageContext (imageSize);
var context = UIGraphics.GetCurrentContext ();
context.SaveState ();
badge.RenderInContext (context);
context.RestoreState ();
context.SetBlendMode (CGBlendMode.Clear);
text.DrawString (bounds, UIFont.BoldSystemFontOfSize (fontSize), UILineBreakMode.Clip);
context.SetBlendMode (CGBlendMode.Normal);
var outputImage = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
outputImage.Draw (rect);
if (Parent.SelectionStyle == UITableViewCellSelectionStyle.None && (Parent.Highlighted || Parent.Selected) && Shadow) {
Layer.CornerRadius = radius;
Layer.ShadowOffset = new SizeF (0f, 1f);
Layer.ShadowRadius = 1f;
Layer.ShadowOpacity = .8f;
} else {
Layer.CornerRadius = radius;
Layer.ShadowOffset = SizeF.Empty;
Layer.ShadowRadius = 0f;
Layer.ShadowOpacity = 0f;
}
}