Forum Xamarin.Forms

Change Fontsize on NSAttributedString

I have a requirment to use a custom renderer to set an Editor to some rtf text. This all works fine. However, what I am not getting at is how to set or change the font size of this NSAttributedString. The constructors dont seem to allow to hand over any additional attributes like font size etc. On the other hand - the NSMutableAttributedString does not offer to add the NSAttributedStringDocumentAttributes . Maybe I am missing something.

var rtfstring = new NSAttributedString(rtftext, new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.RTF }, ref error);
_uITextView.AttributedText = rtfstring;
SetNativeControl(_uITextView);

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You could convert NSAttributedString to NSMutableAttributedString , and then set the font attribute .

    var rtftext = xxx;
    var rtfstring = new NSAttributedString(rtftext, new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.RTF }, ref error);
    
    var mutable = new NSMutableAttributedString(rtfstring);
    UIStringAttributes uiString = new UIStringAttributes();
    uiString.Font = UIFont.SystemFontOfSize(50);
    //override attributes
    mutable.SetAttributes(uiString, new NSRange(0, rtftext.Length));
    
    
    textView.AttributedText = mutable;
    SetNativeControl(textView);
    

    If you want to set font size from Forms project , you could create a new subclass and create custom renderer for the new custom class .

    Custom Class

     public class MyEditor : Editor
        {
            public float AttributeFontSize { get; set; }
        }
    

    Xaml

     xmlns:local="clr-namespace:FormsA"
     <local:MyEditor AttributeFontSize="50" Text="dahihdihfihif" FontSize="50"/>
    

    Custom Renderer

    [assembly: ExportRenderer(typeof(MyEditor), typeof(MyRenderer))]
    namespace FormsA.iOS
    {
        class MyRenderer : EditorRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
    
                if(Control != null)
                {
                    NSError error= null; 
    
                    var rtftext = Element.Text;
                    var rtfstring = new NSAttributedString(rtftext, new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.RTF }, ref error);
    
                    var mutable = new NSMutableAttributedString(rtfstring);
                    UIStringAttributes uiString = new UIStringAttributes();
                    uiString.Font = UIFont.SystemFontOfSize(new nfloat((Element as MyEditor).AttributeFontSize));
                    //override attributes
                    mutable.SetAttributes(uiString, new NSRange(0, rtftext.Length));
    
                    UITextView textView = new UITextView();
                    textView.AttributedText = mutable;
                    SetNativeControl(textView);
                }
    
            }
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • UliAdminUliAdmin Member ✭✭

    Thx this helped a lot., Unfortunatley the NSRange part throws an nsrange exception 'mutable.SetAttributes(uiString, new NSRange(0, rtftext.Length));'

    NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds.

    As the length is 768 this should not be an issue. I could not find any other hint what could cause this exception. I tried already rtftext.Length-1 in case this would be an array index issue - but without luck.

  • ColeXColeX Member, Xamarin Team Xamurai

    @UliAdmin said:
    Thx this helped a lot., Unfortunatley the NSRange part throws an nsrange exception 'mutable.SetAttributes(uiString, new NSRange(0, rtftext.Length));'

    NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds.

    As the length is 768 this should not be an issue. I could not find any other hint what could cause this exception. I tried already rtftext.Length-1 in case this would be an array index issue - but without luck.

    Hi , what is the value of rtftext on your side ? Could you debug and post the value here ?

Sign In or Register to comment.