MvvmCross iOS UILabel AttributedText with Language converter - xamarin

I create a custom control for UILabel with custom Kern parameter.
[Register("CustomLabelView"), DesignTimeVisible(true)]
public class CustomLabelView : UILabel
{
private float _textLetterSpacing;
public CustomLabelView(IntPtr handle) : base(handle)
{
}
[Export("TextLetterSpacing"), Browsable(true)]
public float TextLetterSpacing
{
get => _textLetterSpacing;
set
{
_textLetterSpacing = value;
SetNeedsDisplay();
}
}
public CustomLabelView()
{
Initialize();
}
public override void AwakeFromNib()
{
// Called when loaded from xib or storyboard.
Initialize();
}
void Initialize()
{
NSRange range = new NSRange(0, Text.Length);
var attributes = AttributedText.GetCoreTextAttributes(0, out range);
var attributedTitle = new NSAttributedString(Text, new CTStringAttributes() { KerningAdjustment = TextLetterSpacing, ForegroundColor = TextColor.CGColor, UnderlineStyle = attributes.UnderlineStyle });
AttributedText = attributedTitle;
SetNeedsDisplay();
}
}
I bind text to this control with language converter
set.Bind(CustomLabelView)
.For(c => c.Text)
.To(vm => vm.TextSource)
.WithConversion("Language", "AgreementText");
and underline property doesn't apply for bound text. Also I tried AttributedText property instead of Text. No any changes.
My LinkerPleaseInclude file contains this lines
public void Include(UILabel label)
{
label.Text = label.Text + "";
label.AttributedText = new NSAttributedString(label.AttributedText.ToString() + "");
}
Any ideas how to sort it out?

I changed the UnderlineStyle property's value to one of the enumeration of CTUnderlineStyle and it works for me.
namespace CoreText
{
//
// Summary:
// Specifies the style of an underline ornament.
//
// Remarks:
// To be added.
public enum CTUnderlineStyle
{
None = 0,
Single = 1,
Thick = 2,
Double = 9
}
}
Here is my code:
void Initialize()
{
this.Text = "This is some formatted text";
NSRange range = new NSRange(0, Text.Length);
var attributes = AttributedText.GetCoreTextAttributes(0, out range);
var attributedTitle = new NSAttributedString(Text, new CTStringAttributes() { KerningAdjustment = TextLetterSpacing, ForegroundColor = TextColor.CGColor, UnderlineStyle = CTUnderlineStyle.Single });
AttributedText = attributedTitle;
SetNeedsDisplay();
}
The underline appears,is this you want?

I just put Initialize method into Draw for my CustomLabel.
Here is the solution:
public override void Draw(CGRect rect)
{
Initialize();
base.Draw(rect);
}
private void Initialize()
{
var attributes = AttributedText.GetCoreTextAttributes(0, out _);
AttributedText = new NSMutableAttributedString(Text,
new CTStringAttributes
{
KerningAdjustment = TextLetterSpacing, Font = attributes.Font,
UnderlineStyle = attributes.UnderlineStyle
});
}

Related

xamarin binding variable to label using timer event

I want to binding a variable to a label which as a children in a grid.
the variable changed with a timer event.pls check my code help me found out where i am wrong.I can see the label init value is 0,but it won't update when the variable do.
public class DevicePage : ContentPage
{
IDevice device;
Label testLb = new Label();
System.Timers.Timer testtimer;
Grid gridView;
byte testt { get; set; } = 0;
GetSendData communicate;
private byte _maintext;
public byte MainText
{
get
{
return _maintext;
}
set
{
_maintext = value;
OnPropertyChanged();
}
}
public DevicePage(IDevice currDevice)
{
device = currDevice;
this.Title = "Status";
testtimer = new System.Timers.Timer();
testtimer.Interval = 1000;
testtimer.Elapsed += OnTimedEvent;
testtimer.Enabled = true;
gridView = new Grid();
testLb.Text = testt.ToString();
testLb.SetBinding(Label.TextProperty, ".");
testLb.BindingContext = MainText;
gridView.Children.Add(testLb, 0, 0);
this.Content = gridView;
}
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
MainText += 1;
}
after I implement INotifyPropertyChanged,testLb won't update either.But I change testLb.SetBinding(Label.TextProperty, "."); testLb.BindingContext = MainText; to testLb.SetBinding(Label.TextProperty, "MainText"); testLb.BindingContext = this; It works!

How to set Datepicker default index is string value

I want to show datepicker default index value is string but I am not able to do that. Please suggest any idea. Thanks in advance.
I want to show picker like this:
You can use a UITextField and add date picker as a Input vie. And set PlaceHolder as "Date" or any string you want.
Sample Code
UITextField TextInput = new UITextField();
TextInput.Frame = new CGRect(40, 280, View.Frame.Width - 80, 40);
TextInput.Placeholder = "Date";
var picker = new UIDatePicker();
TextInput.InputView = picker;
Here is an example how to do it in ViewCell. You can use similar idea with View
public class TimePickerInCellPage : ContentPage
{
private ListView _listView;
public TimePickerInCellPage()
{
_listView = new ListView
{
RowHeight = 80,
SeparatorColor = Color.Blue,
SeparatorVisibility = SeparatorVisibility.Default
};
_listView.ItemsSource = new List<TaskTime>() {
new TaskTime { Id=1, StartTime=TimeSpan.FromHours(3) } ,
new TaskTime { Id=1, StartTime=TimeSpan.FromHours(5) } ,
new TaskTime { Id=1, StartTime=TimeSpan.FromHours(7) } ,
};
_listView.ItemTemplate = new DataTemplate(typeof(MyCell));
Content = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { _listView }
};
}
}
public class TaskTime
{
public int Id { get; set; }
public string Task { get; set; }
public TimeSpan StartTime { get; set; }
}
class MyCell : ViewCell
{
private readonly DatePicker _myTimePicker;
//private readonly TimePicker _myTimePicker;
public MyCell()
{
//_myTimePicker = new TimePicker()
_myTimePicker = new DatePicker()
{
HorizontalOptions = LayoutOptions.EndAndExpand
};
//_myTimePicker.Format = "HH:mm:ss";
_myTimePicker.Format = "dd:MM:yy HH:mm:ss";
_myTimePicker.SetBinding(TimePicker.TimeProperty, "StartTime");
_myTimePicker.PropertyChanged += MyTimePicker_PropertyChanged;
//_myTimePicker.Focused += _myTimePicker_Focused;
var viewLayout = new StackLayout()
{
HorizontalOptions = LayoutOptions.StartAndExpand,
Orientation = StackOrientation.Horizontal,
Padding = new Thickness(20),
Children = { _myTimePicker }
};
View = viewLayout;
}
bool firstTimeSet; //set when initial binding happens
//private void _myTimePicker_Focused(object sender, FocusEventArgs e)
//{
// firstTimeSet=true;
//}
private void MyTimePicker_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//if (e.PropertyName == TimePicker.TimeProperty.PropertyName)
if (e.PropertyName == DatePicker.DateProperty.PropertyName)
{
if (!firstTimeSet)
firstTimeSet = true;
else
{
int x = 0;
string s = _myTimePicker.Date.ToString("dd/MM/yy HH:mm:ss");
}
}
}
}

Issue with FlowListView Xamarin.Forms

I am using FlowListView To set gallery view in my xamarin forms application, with following code..
public class Page1 : ContentPage
{
public Page1()
{
ObservableCollection<ItemModel> List = new ObservableCollection<ItemModel>();
string[] images = {
"https://farm9.staticflickr.com/8625/15806486058_7005d77438.jpg",
"https://farm5.staticflickr.com/4011/4308181244_5ac3f8239b.jpg",
"https://farm8.staticflickr.com/7423/8729135907_79599de8d8.jpg",
"https://farm3.staticflickr.com/2475/4058009019_ecf305f546.jpg",
"https://farm6.staticflickr.com/5117/14045101350_113edbe20b.jpg",
"https://farm2.staticflickr.com/1227/1116750115_b66dc3830e.jpg",
"https://farm8.staticflickr.com/7351/16355627795_204bf423e9.jpg",
"https://farm1.staticflickr.com/44/117598011_250aa8ffb1.jpg",
"https://farm8.staticflickr.com/7524/15620725287_3357e9db03.jpg",
"https://farm9.staticflickr.com/8351/8299022203_de0cb894b0.jpg",
};
int number = 0;
for (int n = 0; n < 20; n++)
{
for (int i = 0; i < images.Length; i++)
{
number++;
var item = new ItemModel()
{
ImageUrl = images[i],
FileName = string.Format("image_{0}.jpg", number),
};
List.Add(item);
}
}
FlowListView listView = new FlowListView()
{
FlowColumnTemplate = new DataTemplate(typeof(ListCell)),
SeparatorVisibility = SeparatorVisibility.None,
HasUnevenRows = true,
FlowColumnMinWidth = 110,
FlowItemsSource = List,
};
listView.FlowItemTapped += (s, e) =>
{
var item = (ItemModel)e.Item;
if (item != null)
{
App.Current.MainPage.DisplayAlert("Alert", "Tapped {0} =" + item.FileName, "Cancel");
}
};
Content = new StackLayout
{
Children = {
listView
}
};
}
}
public class ItemModel
{
public string ImageUrl { get; set; }
public string FileName { get; set; }
}
public class ListCell : View
{
public ListCell()
{
CachedImage IconImage = new CachedImage
{
HeightRequest = 100,
Aspect = Aspect.Fill,
DownsampleHeight = 100,
DownsampleUseDipUnits = false,
LoadingPlaceholder = "image_loading.png",
ErrorPlaceholder = "image_error.png"
};
IconImage.SetBinding(CachedImage.SourceProperty, "ImageUrl");
Label NameLabel = new Label
{
Opacity = 0.5,
HorizontalOptions = LayoutOptions.Fill,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.End,
};
NameLabel.SetBinding(Label.TextProperty, "FileName");
Grid grd = new Grid
{
Padding = 3,
ColumnDefinitions = {
new ColumnDefinition { Width = new GridLength (1, GridUnitType.Star) },
},
RowDefinitions = {
new RowDefinition { Height=GridLength.Star},
},
};
grd.Children.Add(IconImage,0,0);
grd.Children.Add(NameLabel, 0, 1);
}
}
i added all the dependency of FlowListView, FFImage etc,
This above code just showing blank screen, Not displaying any data...
You're ListCell has nothing to show.
Append the existing lines to set the content to the grid in your custom ViewCell.
grd.Children.Add(IconImage,0,0);
grd.Children.Add(NameLabel, 0, 1);
Content = grd; <--- add this line
Also have you done the following as per advice from the author? i.e. You've added the nuget library to the platforms in addition to the PCL and added the corresponding initialisation code for the library?
You must add this line to your platform specific project (AppDelegate.cs, MainActivity.cs, etc) before you use FFImageLoading:
CachedImageRenderer.Init();
This is what I got in my IOS:
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
FFImageLoading.Forms.Touch.CachedImageRenderer.Init(); <---- this line
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}

how to use swTableView in Xamarin IOS.

I am totally new to C# and starting with xamarin IOS code. i am basically from native app background.
i wanted simple app showing how to use swTableView controller in xamarin to show table as mention below.
Like :
ColumnNam ColumnNam ColumnNam ColumnNam
data1 data2 data3 data4
data5 data6 data7 data8
i tried search for the example but i did not find one...if any one already has information please let me know..
Thx in advance
There is no system control can perform effect like you need, you must customize one, I write a sample for you to reference:
In ViewController.cs:
public override void ViewDidLoad ()
{
this.Title = "testTalbeView";
this.View.Frame = UIScreen.MainScreen.Bounds;
this.View.BackgroundColor = UIColor.White;
UITableView tableView = new UITableView (UIScreen.MainScreen.Bounds);
tableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
tableView.Source = new MyTableSource ();
this.Add (tableView);
}
MyTableSource.cs:
public class MyTableSource : UITableViewSource
{
private string cellID = "MyCell";
private int columns = 2;
private List<string> dataList;
public MyTableSource ()
{
dataList = new List<string> ();
for (int i = 0; i < 10; i++) {
dataList.Add ("data " + i.ToString ());
}
}
#region implemented abstract members of UITableViewSource
public override nint RowsInSection (UITableView tableview, nint section)
{
return dataList.Count / columns + 1;
}
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
MyCell cell = tableView.DequeueReusableCell (cellID) as MyCell;
if (null == cell) {
cell = new MyCell (UITableViewCellStyle.Default, cellID);
cell.TextLabel.TextAlignment = UITextAlignment.Center;
}
int row = (int)indexPath.Row;
if (0 == row) {
cell.SetData ("Column0", "Column1");
}
else{
cell.SetData (dataList [(row-1) * columns], dataList [(row-1) * columns + 1]);
}
return cell;
}
#endregion
}
MyCell.cs:
public class MyCell : UITableViewCell
{
private UILabel lbC0;
private UILabel lbC1;
public MyCell (UITableViewCellStyle style,string cellID):base(style,cellID)
{
lbC0 = new UILabel ();
lbC0.TextAlignment = UITextAlignment.Center;
this.AddSubview (lbC0);
lbC1 = new UILabel ();
lbC1.TextAlignment = UITextAlignment.Center;
this.AddSubview (lbC1);
}
public void SetData(string str0,string str1)
{
lbC0.Text = str0;
lbC1.Text = str1;
}
public override void LayoutSubviews ()
{
nfloat lbWidth = this.Bounds.Width / 2;
nfloat lbHeight = this.Bounds.Height;
lbC0.Frame = new CoreGraphics.CGRect (0, 0, lbWidth, lbHeight);
lbC1.Frame = new CoreGraphics.CGRect (lbWidth, 0, lbWidth, lbHeight);
}
}
Then you can get the tableView like the picture:
Hope it can help you.

Trouble applying custom Core Image Filter to CALayer's Filters property

NOTE: I'm using Xamarin.Mac, but I believe the intent and/or missteps should be clear enough to most swift Cocoa developers.
PROBLEM:
My custom filter is not being applied to the view it backs.
EXAMPLE
var builtInFilter = new CIColorInvert();
builtInFilter.SetDefaults();
var customFilter = new HazeFilter();
customFilter.SetDefaults();
//In both cases here, the Image and OutputImage properties will have a null value
Layer.Filters = new CIFilter[1]{builtInFilter}; //Works
Layer.Filters = new CIFilter[1]{customFilter}; //Does nothing
The problem isn't that my custom filter isn't capable of doing anything. When I assign its Image property and draw its OutputImage it works as expected.
This tells me that the Kernel and OutputImage method are functioning properly.
Source Code
HazeFilter.cs
public class HazeFilter : CIFilter
{
static CIKernel hazeRemovalKernel;
public HazeFilter () : base()
{
if (hazeRemovalKernel == null) {
hazeRemovalKernel = CIKernel.FromProgramSingle(#"
kernel vec4 myHazeRemovalKernel(sampler src, __color color, float distance, float slope)
{
vec4 t;
float d;
d = destCoord().y * slope + distance;
t = unpremultiply(sample(src, samplerCoord(src)));
t = (t-d*color)/(1.0-d);
return premultiply(t);
}");
}
}
public override void SetDefaults ()
{
base.SetDefaults ();
inputColor = CIColor.FromCGColor (NSColor.Purple.CGColor);
inputDistance = 0.8;
inputSlope = 0.002;
}
CIImage image;
[Export("inputImage")]
public new CIImage Image
{
get { return image; }
set {
WillChangeValue ("inputImage");
image = value;
DidChangeValue ("inputImage");
}
}
CIColor color;
[Export("inputColor")]
public CIColor inputColor
{
get { return color; }
set {
WillChangeValue ("inputColor");
color = value;
DidChangeValue ("inputColor");
}
}
NSNumber distance;
[Export("inputDistance")]
public NSNumber inputDistance{
get { return distance; }
set {
WillChangeValue ("inputDistance");
distance = value;
DidChangeValue ("inputDistance");
}
}
NSNumber slope;
[Export("inputSlope")]
public NSNumber inputSlope{
get { return slope; }
set {
WillChangeValue ("inputSlope");
slope = value;
DidChangeValue ("inputSlope");
}
}
[Export("outputImage")]
public new CIImage OutputImage
{
get
{
if (Image == null)
return null;
var inputSampler = new CISampler (Image);
var argumentArray = NSArray.FromNSObjects (new NSObject[] {
inputSampler,
Runtime.GetNSObject (inputColor.Handle),
inputDistance,
inputSlope
});
return Apply (hazeRemovalKernel, argumentArray, null);
}
}
}
Canvas.cs
public class Canvas : NSView
{
public Canvas (CGRect rect) : base(rect)
{
WantsLayer = true;
Layer.BackgroundColor = NSColor.Clear.CGColor;
Layer.MasksToBounds = true;
Layer.NeedsDisplayOnBoundsChange = true;
LayerUsesCoreImageFilters = true;
}
CIFilter effect;
public CIFilter Effect {
get { return effect; }
set{
effect = value;
Layer.Filters = new CIFilter[1]{Effect};
NeedsDisplay = true;
}
}
}
Instantiation
var builtInFilter = new CIColorInvert();
builtInFilter.SetDefaults();
var customFilter = new HazeFilter();
customFilter.SetDefaults();
wrapPanel.Effect = builtInFilter; //Works as expected
wrapPanel.Effect = customFilter; //Does nothing

Resources