Set Navigation Title color in Xamarin(Ios) - c#

I want set navigation Bar Title color in Xamarin(Ios) using C#.
I set below way but it not work.
this.NavigationController.NavigationBar.BarTintColor = UIColor.Clear.FromHexString("#0072BA", 1.0f);
this.NavigationController.NavigationBar.TitleTextAttributes = UIColor.Magenta;
the BarTintColor work but TitleTextAttributes not work.. I also see documentation but in document the title text color not mention.
Any help be Appreciated..

After Spending 1 Hour I got the solution.
Type UIKit.UINavigationBar does not contain a definition for SetTitleTextAttributes and no extension method SetTitleTextAttributes of type UIKit.UINavigationBar could be found. and it give below error :
Are you missing an assembly reference ?
So Instead of calling SetTitleTextAttributes, do the following:
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
{
ForegroundColor = UIColor.White
};
Hope this will help other.

You can use a custom view as the navigation bar's title view, then you can modify it easily.
In your "ViewDidLoad" method, using this code:
public override void ViewDidLoad()
{
//Normal way
//this.Title = "Normal";//or this.NavigationItem.Title = "Normal;
//Using custom view
UILabel lbTitle = new UILabel(new CoreGraphics.CGRect(0, 0, 50, 30));
lbTitle.Text = "CustomView";
lbTitle.TextColor = UIColor.Red;
lbTitle.Font = UIFont.SystemFontOfSize(15);
this.NavigationItem.TitleView = lbTitle;
//Using a picture
//this.NavigationItem.TitleView = new UIImageView();
}
Hope it can help you.

Related

Xamarin change searchbar underline color and Icon Spacing Fix [duplicate]

Pretty straight-forward, how do I change the color of the line along the bottom of a SearchView in Android? I've tried, queryBackground, background, and various other things but cant seem to get it in XML. Im happy doing it programmatically or in XML. Just want it to work!
Cheers
Use Below method to change colour of bottom line of SearchView.
Here I add Blue Colour as bottom Line
private void customizeSearchView() {
int searchTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchBox = ((EditText) searchView.findViewById(searchTextId));
searchBox.setBackgroundColor(Color.WHITE);
searchBox.getLayoutParams().height = LinearLayout.LayoutParams.MATCH_PARENT;
int search_plateId = getResources().getIdentifier("android:id/search_plate", null, null);
View mSearchPlate = ((View) searchView.findViewById(search_plateId));
mSearchPlate.setBackgroundColor(Color.BLUE);
int searchCloseImageId = getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView searchClose = ((ImageView) searchView.findViewById(searchCloseImageId));// change color
searchClose.setBackgroundColor(Color.WHITE);
}
hi try this it may help you
android.support.v7.widget.SearchView searchView=findViewById(R.id.search_view);
View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("color code"));
androidx.appcompat.widget.SearchView searchView = findViewById(R.id.Search_Categorie);
View v = searchView.findViewById(androidx.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("#ffffff"));

Font Size change KerningAdjustment, why?

I solved the problem, I created a function where I just pass the value of the spacing I need and the TextField
public static void ChangeSpaceBetweenCharacters(this UITextField textField, float space)
{
textField.WeakDefaultTextAttributes.SetValueForKey(NSObject.FromObject((nfloat)space), UIStringAttributeKey.KerningAdjustment);
}
Best regards,
Rafael Santos
I think the problem may be caused by the font method you use, I can't make it work with:
UIFont("ArialMT", 13)
Instead, you can use :
Font = UIFont.FromName("ArialMT", 13),
Or
Font = UIFont.SystemFontOfSize(10),
I test below sample code and it works for me:
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
NSMutableAttributedString mtText = new NSMutableAttributedString("testTextfield", new UIStringAttributes
{
Font = UIFont.SystemFontOfSize(10),
KerningAdjustment = 8.0f
});
UITextField textField = new UITextField();
textField.Frame = new CoreGraphics.CGRect(10,20,350,50);
textField.AttributedText = mtText;
Add(textField);
}

How do I switch pages within a WPF application?

I have a WPF application and I cannot figure out a way to switch pages, even create multiple pages. All of the questions related to this topic only show how to switch, not create AND switch.
I have already tried to look up the solution, but no proper answer shows up.
Try
this.NavigationService.Navigate(new PageName());
Or
this.NavigationService?.Navigate(new PageName()); //To check if it's not null
Edit:Sorry, I didn't understand your question correctly.
As I know, you can't just create and navigate to a page. Why can't you just create a page, design it however you want, and then navigate?
Edit2: After some research, I found that you can create your own class and inherit it from Window class.
public class MyPage: Window
{
this.WindowStyle = WindowStyle.SingleBorderWindow;
//Add grid
this.RootGrid = new Grid()
{
//Add TextBox
TextBox TextBox_Test = new TextBox()
{ Text = "ABC",
Background = Brushes.Yellow,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top
};
this.RootGrid.Children.Add(TextBox_Test);
}
}
You can add rows/columns as well.
this.RootGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
this.RootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width= new GridWidth(20) });
Then you can navigate to MyPage page as I told.
Hope this helps.

Image from ImageSource

I tried to follow the following example by #CraigDunn with the sample code
someButton.Image = ImageSource.FromFile("imageName.png");
My own try looks like this:
_NavBarPrevPage = new Button
{
Image = ImageSource.FromResource("media_step_back.png")),
BackgroundColor = Color.Red,
TextColor = Color.White,
};
For me, the IDE tells me:
The type Xamarin.Forms.ImageSource can't be converted to Xamarin.Forms.FileImageSource. An explicit conversion exists. Perhaps a conversion is missing.
I think I'm doing exactely what #CraigDunn proposed.
What am I doing wrong?
I've tried the following, and I was able to get rid of the conversion errors in the current answer, but the image is not shown on the button, while it is shown in an image, so the file name is correct:
var nImg = new FileImageSource()
{
File = "media_step_back.png",
};
_NavBarPrevPage = new Button
{
Image =nImg,
};
If the thing you need is a button with image that is implementing by code-behind :
Button button = new Button
{
BackgroundColor = Color.Red,
Font = Font.Default,
FontSize = 10,
TextColor = Color.White,
HeightRequest = 35,
WidthRequest = 80
};
button.Image = "media_step_back.png";
this.Content = button;
Please remember to put the image file (media_step_back.png) in your target platform's resource folder
UPDATE:
You problem were you didn't add the image source in the Property folder and target platform's resource folder (Please note that in below example, I have added a xamarin.png image in each Xamarin "Property" folder, Android's "Resource" and iOS's "Resource" folder)
Below is the updated code that I placed the button into a StackLayout (which also able to works if without StackLayout) :
public class MainPageNew : ContentPage
{
public MainPageNew()
{
StackLayout parent = new StackLayout();
Button button = new Button
{
BackgroundColor = Color.Red,
Font = Font.Default,
FontSize = 10,
TextColor = Color.White,
HeightRequest = 300,
WidthRequest = 80
};
//button.Image = "media_step_back.png";
button.Image = "xamarin.png";
parent.Children.Add(button);
this.Content = parent;
}
Android
iOS:
This is the download link of the updated code.

C# XNA if-else and defaulting to first value

So I am developing a custom ButtonGUI class for my game. Here's the initialization of the button object:
// Button code:
ButtonGUI btn1 = new ButtonGUI("Button 1", new Rectangle(150, 300, (int)myFont.MeasureString(menuButtons[0]).X, (int)myFont.MeasureString(menuButtons[0]).Y), myFont, Color.CornflowerBlue);
Now consider this code:
// Draw() method:
btn1.Draw(spriteBatch);
if (btnHover)
{
btn1.btnRect = new Rectangle(140, 300, (int)hoverFont.MeasureString(menuButtons[0]).X, (int)hoverFont.MeasureString(menuButtons[0]).Y);
btn1.btnFont = hoverFont;
btn1.btnColour = Color.Red;
}
else
{
btn1.btnRect = new Rectangle(150, 300, (int)myFont.MeasureString(menuButtons[0]).X, (int)myFont.MeasureString(menuButtons[0]).Y);
btn1.btnFont = myFont;
btn1.btnColour = Color.CornflowerBlue;
}
This would be OK if I had only 1 button... But if I have like 10 buttons or more? This really isn't what DRY suggests. I feel like I'm missing something, there must be a way to return button properties to their default values once the condition is no longer met without doing the whole thing manually, or is there? Thanks in advance!
It may make sense to create a structure to hold all of the values that may change.
class ButtonData
{
// put members corresponding to each member of ButtonGUI you wish
// to change
}
class ButtonSwapper
{
ButtonGUI myButton;
ButtonData hoverData;
ButtonData notHoverData;
void change(bool hover)
{
ButtonData dataToUse = hover ? hoverData : notHoverData;
// set each relevant member of myButton to its pair in
// dataToUse
}
}
then call change as necessary.

Categories