The smart way to do it is to create a custom label. Since custom labels are tricky we have created one for you - it anchors to the bottom side of the shape to which you add it. The label automatically grows to fit the text size and takes into account the shape orientation. Following is the code of the label:
[Serializable]
public class NBottomSideLabel : NLabel
{
#region Constructors
///
/// Default constructor
/// public NBottomSideLabel()
{
}
///
/// Initializer constructor
/// ///
///
public NBottomSideLabel(string text, Guid anchorUniqueId)
: base(text, anchorUniqueId)
{
}
#endregion
#region Interface implementations
#region INDiagramElementReferenceHolder overrides
///
/// Provides a filter for the specified id property
/// ///
/// For the AnchorUniqueId property this implementation will return the NFilters.TypeNModel filter
/// ///
property exposing id, for which to obtain filter
///
filterpublic override NFilter GetFilterForReferenceProperty(string property)
{
if (property != "AnchorUniqueId")
throw new ArgumentException("Can only provide filter for the AnchorUniqueId property", "property");
return NFilters.TypeNModel;
}
#endregion
#region INText overrides
///
/// Obtains text paint info in world coordinates
/// ///
text for which to obtain paint info
///
result paint info
///
true if paint info was successfully obtained, otherwise falsepublic override bool GetWorldTextPaintInfo(string text, out NTextPaintInfo info)
{
info = new NTextPaintInfo();
// get the anchor model
NModel anchor = GetAnchorModel(AnchorUniqueId);
if (anchor == null)
return false;
// get model bounds bottom side position in world coordinates
NRectangleF modelBounds = anchor.ModelBounds;
NPointF bottomLeft = new NPointF(modelBounds.X, modelBounds.Bottom);
NPointF bottomRight = new NPointF(modelBounds.Right, modelBounds.Bottom);
NPointF bottomCenter = new NPointF(modelBounds.Center.X, modelBounds.Bottom);
// transform local 2 scene
NMatrix2DF sceneTransform = anchor.SceneTransform;
bottomLeft = sceneTransform.TransformPoint(bottomLeft);
bottomRight = sceneTransform.TransformPoint(bottomRight);
bottomCenter = sceneTransform.TransformPoint(bottomCenter);
// transform scene 2 world
INWorld world = (RootNode as INWorld);
if (world != null)
{
bottomLeft = world.SceneToWorld.TransformPoint(bottomLeft);
bottomRight = world.SceneToWorld.TransformPoint(bottomRight);
bottomCenter = world.SceneToWorld.TransformPoint(bottomCenter);
}
// get the text size in world coordinate using current text style
NSizeF size;
if (MeasureStringInWorld(text, ComposeTextStyle(), out size) == false)
return false;
// determine the rotation angle of the text in world coordinates
float angle = (float)Math.Atan2(bottomRight.Y - bottomLeft.Y, bottomRight.X - bottomLeft.X);
// init the info
info.Origin = new NPointF(
bottomCenter.X + (float)Math.Cos(angle + Math.PI / 2) * (size.Height / 2),
bottomCenter.Y + (float)Math.Sin(angle + Math.PI / 2) * (size.Height / 2));
info.Size = size;
info.Orientation = angle * 180.0f / (float)Math.PI;
info.Mode = PaintTextMode.Flow;
return true;
}
#endregion
#endregion
}
The following code creates a rectangle with this label:
NRectangleShape shape = new NRectangleShape(0, 0, 100, 100);
shape.Labels.RemoveAllChildren();
shape.Labels.AddChild(new NBottomSideLabel("hello there", shape.UniqueId));
Best Regards,
Nevron Support Team