Реализовал сэмпл как вставлять знак рубля в программу

This commit is contained in:
Rustam Zaitov 2014-02-05 21:52:13 +04:00
parent 32a6e7a372
commit 45c281cfee
4 changed files with 89 additions and 6 deletions

View File

@ -1,18 +1,24 @@
using System;
using System.Drawing;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.Foundation;
namespace Rouble
{
public class MainController : UIViewController
{
private const string Text = "Цена: 100 {0}";
private const string Text = "Цена: 100 ";
private UILabel _label1;
private UILabel _label2;
private UILabel _label3;
private UIFont _font1;
private UIFont _font2;
private UIFont _font3;
private UIFont _roubleFont;
public MainController ()
{
@ -23,37 +29,57 @@ namespace Rouble
{
base.ViewDidLoad ();
InitFonts ();
View.BackgroundColor = UIColor.White;
_label1 = new UILabel ();
_label1.Text = Text;
SetText (_label1, _font1, RoubleType.Regular);
View.AddSubview (_label1);
_label2 = new UILabel ();
_label2.Text = Text;
SetText (_label2, _font2, RoubleType.Medium);
View.AddSubview (_label2);
_label3 = new UILabel ();
_label3.Text = Text;
SetText (_label3, _font3, RoubleType.Bold);
View.AddSubview (_label3);
}
private void InitFonts()
{
_font1 = UIFont.FromName ("HelveticaNeue", 18f);
_font2 = UIFont.FromName ("HelveticaNeue-Medium", 18f);
_font3 = UIFont.FromName ("HelveticaNeue-Bold", 18f);
_roubleFont = UIFont.FromName("Ruble", 18f);
}
public override void ViewDidLayoutSubviews()
{
base.ViewWillLayoutSubviews ();
_label1.SizeToFit ();
_label1.Center = View.Center;
MoveY (_label1, -50f);
_label2.SizeToFit ();
_label2.Center = View.Center;
MoveY (_label2, -50f);
_label3.SizeToFit ();
_label3.Center = View.Center;
MoveY (_label3, 50f);
}
private void SetText(UILabel label, UIFont font, RoubleType type)
{
char roubleSym = Roubles.GetRoubleSymbFor (type);
NSMutableAttributedString attrString = new NSMutableAttributedString();
attrString.Append(new NSAttributedString(Text, font: font, foregroundColor: UIColor.Black));
attrString.Append(new NSAttributedString(roubleSym.ToString(), font: _roubleFont, foregroundColor: UIColor.Black));
label.AttributedText = attrString;
}
private void MoveY(UIView view, float dy)
{
RectangleF frame = view.Frame;

View File

@ -98,6 +98,12 @@
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="MainController.cs" />
<Compile Include="..\Shared\Rouble.cs">
<Link>Rouble.cs</Link>
</Compile>
<Compile Include="..\Shared\RoubleType.cs">
<Link>RoubleType.cs</Link>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

40
Rouble/Shared/Rouble.cs Normal file
View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
namespace Rouble
{
public static class Roubles
{
private static readonly Dictionary<RoubleType, char> _roubleSymbols;
public static char RegularSymbol
{
get { return GetRoubleSymbFor(RoubleType.Regular); }
}
public static char MediumSymbol
{
get { return GetRoubleSymbFor(RoubleType.Medium); }
}
public static char BoldSympbol
{
get { return GetRoubleSymbFor(RoubleType.Bold); }
}
static Roubles()
{
_roubleSymbols = new Dictionary<RoubleType, char>(3)
{
{ RoubleType.Regular, 'r' },
{ RoubleType.Medium, 'm' },
{ RoubleType.Bold, 'b' },
};
}
public static char GetRoubleSymbFor(RoubleType type)
{
return _roubleSymbols[type];
}
}
}

View File

@ -0,0 +1,11 @@
using System;
namespace Rouble
{
public enum RoubleType
{
Regular,
Medium,
Bold
}
}