How to set same font family to entire android application - android-actionbar

Hello I want to set same font family to entire Android application. I have seen this Is it possible to set a custom font for entire of application? but this all doing using code. I want to set in Theme such that it will work in all. It should convert the font family of ActionBar text also.
Any way to do this ?
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

If you want to set the TypeFace for all of your Views without having to do it programatically each time (and still work across all versions of Android), your best option would be to subclass the View and have it automatically set the TypeFace you want.
IE.
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init(boolean bold) {
setTypeface(Typeface.createFromAsset(getAssets(), "fonts/your_font_file.ttf"));
}
}
If you want to really optimize that even further, you can use a static reference to that TypeFace and use that so you don't need to recreate the TypeFace every time the View loads.

Related

VSTO 4/XML: how to make certain controls optionally visible

I've a XML Ribbon made by VSTO-4 and VS2017 functioning in Outlook-2016. I'm not using the Designer provided by Visual Studio, but the entire "Fluent" mode (XML).
This Ribbon if fulfilled with buttons and my clients are "a little bit" lost with so many buttons/options in the same ribbon... and most of them are really just "options" of my program.
I would like to make this Ribbon (named now Ribbon-1) with just 3 buttons, one of them an "OPTIONS" command-button, to call the Ribbon-2, this one filled with all other buttons I have now in Ribbon-1.
Obviously, Ribbon-2 will appear at the same TAB of Ribbon-1 (as least "appearing be in the same TAB") and, once the user set an option, he click on "BACK" button and Ribbon-2 disappear and Ribbon-1 appears again...
We can see this behaviour in some AddIns and I would like to make the same.
Any suggestion?
I appreciate any tip.
Rather than using multiple Ribbons it might make sense to put all the controls in one Ribbon. Use the getVisible attribute to set the visibility of all buttons and groups that should optionally be hidden or visible. Use a toggleButton to show/hide these buttons.
The onAction callback for the toggleButton can set a class-level variable that the getVisible callbacks can check. The procedure then invalidates the Ribbon so that the getVisible callbacks are triggered. These, in turn, check the class-level variable to determine the visibility state of each button.
Note that get callbacks are also executed when the Ribbon loads.
Sample Ribbon XML:
<group id="MyGroup" label="TEST empty" visible="true">
<button id="testButton" label="test empty" visible="true"/>
<toggleButton id="testToggle" label="toggle optional buttons" visible="true" onAction="toggleVisibleControls"/>
<button id="optionalButton" label ="optional" getVisible="isVisible" />
</group>
<group id="Optional" label="Optional group" getVisible="isVisible"></group>
Sample VB.NET code for a VSTO Ribbon XML:
'Generated by VSTO
<Runtime.InteropServices.ComVisible(True)> _
Public Class Ribbon1
Implements Office.IRibbonExtensibility
Private ribbon As Office.IRibbonUI
Private ShowHide As Boolean = False
Public Sub New()
End Sub
Public Function GetCustomUI(ByVal ribbonID As String) As String Implements Office.IRibbonExtensibility.GetCustomUI
Return GetResourceText("VB2010addin_RibbonXML.Ribbon1.xml")
End Function
#Region "Ribbon Callbacks"
'Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1.
Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
Me.ribbon = ribbonUI
End Sub
Public Function isVisible(ByVal control As Office.IRibbonControl) As Boolean
Return Me.ShowHide
End Function
Public Sub toggleVisibleControls(ByVal control As Office.IRibbonControl, pressed As Boolean)
ShowHide = pressed
ribbon.Invalidate()
End Sub
#End Region

Prevent FOUC in context of a Kentico 10 website

Zurb Foundation recommends adding class="no-js" to the page html tag to prevent against Flash of Unstyled Content (FOUC).
We are using Kentico CMS. I'm looking for the "Kentico way" of adding a class to the html element tag in the master page. We are using the CMS portal engine.
What we're going for
<html class="no-js"
Willing to fall back into ASP.NET if there's not a more approachable way using Kentico macros or its API.
On your master page portal template you can add some code like this to make that modification:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (CurrentDocument != null)
{
CMS.UIControls.ContentPage page= this.Page as CMS.UIControls.ContentPage;
if (page != null)
{
page.XmlNamespace += " class='no-js'";
}
}
}
</script>
You can also modify the \CMSPages\PortalTemplate.aspx page as well but that is not supported or recommended by Kentico as it typically breaks the upgrade path.
The quickest way I know of is to modify CMSPages\PortalTemplate.aspx to add your class in there.
<%# Page Language="C#" AutoEventWireup="true" Inherits="CMSPages_PortalTemplate" ValidateRequest="false" MaintainScrollPositionOnPostback="true" EnableEventValidation="false" Codebehind="PortalTemplate.aspx.cs" %>
<%=DocType%>
<html <%=XHtmlNameSpace%> <%=XmlNamespace%> class="no-js">
<head id="head" runat="server" enableviewstate="false">
...
The “no-js” is à default value. If, indeed, your browser DOES SUPPORT JavaScript, the “no-is" class will be stripped by the Modernizr engine.
Modernizr is a JavaScript library and it will check the browser capabilities and update the html class="” mention.
For example, if your browser supports the flexbox technology, the flexbox class will be added to the html class list. Otherwise, no-flexbox class will be added.
Later, when using CSS framework (Bootstrap or Foundation), some strategies could be used to emulate a missing feature.

How to prevent Sitecore rich text editor from adding span and style attributes

We have a very restricted rich text editor for our client. We only allow a handful of standard tags. We want all styling to be dictated by the CSS we've built for the site. They are not allowed to deviate.
We've removed most options form the toolbar and only having specific tags in the dropdown, but we recently added CSS to the editor so that the text in the editor is styled like it is on the page to make it easier for authors to visualize. Now under certain circumstances, it will insert span tags with inline styles like:
<h2><span style="font-family: Georgia, 'Times New Roman', serif; color: #232b37;">text</span></h2>
It seems to be pulling our CSS in. Is there a way to configure it to just not do this? Thanks.
No, you cannot change this behavior by configuration. What you could do is add an item:saving event handler, that removes any unwanted tags like these span tags whenever a content editor saves the item. It is not the nicest solution but at least it works. I often use it to clean out unwanted (empty) paragraph tags the rich text editor tends to add.
<event name="item:saving">
<handler type="ExampleProject.Events.RemoveUnwantedTags, ExampleProject" method="OnItemSaving" />
</event>
Removing empty P-tags is easy, but for your solution you could use a regex to replace any tags with an inline style attribute (which should be unique anyway).
Here's a mockup code template you could use to replace all rich text editor field values in an item for this event handler:
public class RemoveUnwantedTags
{
public void OnItemSaving(object sender, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
if (item == null)
{
return;
}
foreach (Field field in item.Fields)
{
if (!field.TypeKey.Equals("rich text", StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
var content = field.Value;
if (!string.IsNullOrEmpty(content))
{
content = content.Trim();
// replace whatever you want over here
using (new SecurityDisabler())
{
item.Editing.BeginEdit();
field.Value = content;
item.Editing.EndEdit();
}
}
}
}
}
Of course it's tempting to make all sort of item:save pipelines because they just work. But don't forget about more specific pipelines that can actually hook into the moment after you click on the Accept button and the moment Sitecore put's the richtext in the field.
<!-- Transforms markup from rich text fields before the Rich Text Editor loads it. -->
<loadRichTextContent/>
<!-- Transforms markup from the Rich Text Editor before saving it as a rich text field value. -->
<saveRichTextContent/>
Both are pipelines that work great for this purpose.
An example implementation of this can be found at https://techmusingz.wordpress.com/2014/06/14/wrapping-rich-text-value-in-paragraph-tag-in-sitecore/
copy/paste from the website mentioned above:
public void Process(SaveRichTextContentArgs args)
{
if (!(args.Content.Trim().StartsWith("<p>") && args.Content.Trim().EndsWith("</p>")))
args.Content = "<p>" + args.Content + "</p>";
}
I personally don't like real string operations on the output so I would recommend handling the richtext content with HtmlAgilityPack (which Sitecore comes with) or use a XDocument.
Example by myself with HtmlAgilityPack that removes script and style elements:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
// Strip output from all kinds of stuff
doc.DocumentNode.Descendants()
.Where(n => n.Name == "script" || n.Name == "style")
.ToList()
.ForEach(n => n.Remove());
An even better solution would be to parse and clean it up with a good and solid HtmlSanitizer.
https://github.com/mganss/HtmlSanitizer
Make sure you create some unittests to wrap the pipeline and test it to its extends.
Best Regards,
Alex van Wolferen

Add enclosing tag for only UnOrdered list from Rich Text Editor in UL

I need to style UL's coming from Rich Text Editor in Sitecore. I am trying to find out if there is a class that I can add to all UL's coming from Sitecore's Rich Text Editor.
Thanks in Advance
Ashok
The easiest solution is just to wrap your FieldRenderer with an HTML element with appropriate class applied in code:
<div class="rich-text">
<sc:FieldRenderer ID="frRichTextField" runat="server" FieldName="MyFieldName" />
</div>
And then add in some CSS styles to handle your UL's within this:
.rich-text ul {
/* add in your styling */
}
You can also use the before and after properties of the FieldRenderer to pass in your tag:
<sc:FieldRenderer ID="frRichTextField" runat="server" FieldName="MyFieldName"
Before="<div class='rich-text'>" After="</div>" />
EDIT:
If you wanted to be more drastic then you could add in your own renderField pipeline processor to ensure your control is always wrapped with the required tag or you could make use of the enclosingTag property and patch the AddBeforeAndAfterValues pipeline instead:
namespace MyCustom.Pipelines.RenderField
{
public class AddBeforeAndAfterValues
{
public void Process(RenderFieldArgs args)
{
Assert.ArgumentNotNull((object)args, "args");
if (args.Before.Length > 0)
args.Result.FirstPart = args.Before + args.Result.FirstPart;
if (args.After.Length > 0)
{
RenderFieldResult result = args.Result;
string str = result.LastPart + args.After;
result.LastPart = str;
}
if (args.EnclosingTag.Length == 0 || args.Result.FirstPart.Length <= 0 && args.Result.LastPart.Length <= 0)
return;
// check if a css class paramter has been passed in
string cssClass = args.Parameters.ContainsKey("class") ? args.Parameters["class"] : String.Empty;
// add the class to the enclosing tag property
args.Result.FirstPart = StringExtensions.FormatWith("<{0} class='{1}'>{2}", (object)args.EnclosingTag, cssClass, (object)args.Result.FirstPart);
args.Result.LastPart = StringExtensions.FormatWith("{0}</{1}>", (object)args.Result.LastPart, (object)args.EnclosingTag);
}
}
}
Patch the Sitecore config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<renderField>
<processor type="Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel"
set:type="MyCustom.Pipelines.RenderField.AddBeforeAndAfterValues, MyCustom.Pipelines" />
</renderField>
</pipelines>
</sitecore>
</configuration>
And then call the FieldRenderer with the EnclosingTag set and pass in your class parameter:
<sc:FieldRenderer ID="frRichTextField" runat="server" FieldName="MyFieldName"
EnclosingTag="div" Parameters="class=rich-text" />
This really doesn't add much over using the before/after properties though and I would generally try to stay away from overwriting default Sitecore processors to save heartache when upgrading.
You could either tap into relevant pipelines or update your sublayouts so that you always have a fixed class around every instance of the rich text field rendering:
<div class="rtf">
<sc:Text ID="scContent" runat="server" FieldName="Content" />
</div>
You will have to make sure as a developer that all current and future instances of rich text field renderings are enclosed by a tag with this class.
You could then include in the global CSS, a common style for this class.
.rtf ul {
...
....
}
If you don't want to have to add this wrapper for every single rtf rendering, you could tap into a relevant pipeline. (Note - this might be a better approach with regard to code maintainability)
You could choose to use one of the two:
renderField pipeline
or the
saveRichTextContent pipeline
So you would add a new processor for either of these pipelines, in which you could access the text within rich text fields only and process that as you please (easier to manipulate the html using the html agility pack)
If you use renderField pipeline - the text within the rich text field in sitecore will not change, the code you write will execure only while rendering the field - in preview / page editor or normal mode.
Using saveRichTextContent pipeline on the other hand, will update the in the rich text field when the content author clicks on save (after entering the text) in content editor mode.
You can see the following examples for these:
renderField - http://techmusingz.wordpress.com/2014/05/25/unsupported-iframe-urls-in-sitecore-page-editor-mode/ (Sample of HtmlUtility is also present here - instead of selecting all tags, you could select all and add your desired class attribute.)
saveRichTextContent - http://techmusingz.wordpress.com/2014/06/14/wrapping-rich-text-value-in-paragraph-tag-in-sitecore/
Hope this helps.
Best practice would be to just add the Class to the Rich Text Editor for use in the Editor.
There are lots of good articles on doing this. Here are a couple:
http://sitecoreblog.blogspot.com/2013/11/add-css-class-to-richtext-editor.html
http://markstiles.net/Blog/2011/08/13/add-css-classes-to-sitecore-rich-text-editor.aspx
These are minimal changes that provide you with the ability to put the styling ability in the hands of the Content Author, but still controlling what styles and classes they can use inline.

Accessing Sitecore Properties for an Image field in user control's Code-behind

I want to access (and then modify) the properties for the image field(s) in Sitecore (esp. interested in 'Dimensions' and 'Keep Ratio' check box) via a code-behind file (blah.ascx.cs).
[This to ensure that all the images have a consistent size, when displayed in the sublayouts (irrespective of the original size)]
I know these properties can be added manually, but for the case where content authors "forget" to add the dimensions for image(s), I don't want my sublayouts to screw up. Hence, I need a check for that.
Is there a way to access and then modify these properties via code?
Here is an image that shows exactly what I want to access and modify via code:
If you're using an <asp:Image ... /> and binding the ImageUrl via C#, you can access the ImageField in C# and get the properties, like so:
Sitecore.Data.Fields.ImageField field = item.Fields["My Image Field"];
// you can now access:
// field.Width
// field.Height etc...
If you're using a <sc:Image ... /> renderer or a regular <sc:FieldRenderer ... /> I would recommend you use <sc:FieldRenderer ... /> as the image parameters only work on that, as documented here.
Here's a sample snippet:
<sc:FieldRenderer FieldName="My Image Field" Parameters="w=100&h=200&as=1" runat="server" />
This to ensure that all the images have a consistent size, when displayed in the sublayouts (irrespective of the original size
I'd suggest to use FieldRenderer class, or <sc:image> for rendering such fields in sublayouts, and then specify "mh" (max height) and "mw" (max width) parameters, so they will always fit into layout.
You can read more about parameters here - http://sdn.sitecore.net/Articles/XSL/5%203%20Enhancements/Image%20Enhancements.aspx
Not a 100% sure (can't check here), but if you retrieve the field as an ImageField you should be able to access those values.
var cur = Sitecore.Context.Item;
Sitecore.Data.Fields.ImageField imgfield = cur.Fields["imagefieldname"] as Sitecore.Data.Fields.ImageField;
//imgfield.<properties>