locked
Bold & Non-Bold Text In TextView RRS feed

  • Question

  • User389827 posted

    I'm Using textview for showing some detail. I need to achieve the text format as below.

    How ? Some text

    Who? Some text

    Text with the question mark should be Bold letter and other text will be normal. Same for for the other text in the textview

    I'm working on multiple language text with the same format.

    Can Someone please help me on how to get this format in Xamarin iOS text view. Thanks !!

    Wednesday, June 24, 2020 10:07 AM

Answers

  • User369979 posted

    Add another attribute for the "Who?"

    UITextView textView = new UITextView(new CGRect(0, 100, 100, 200));
    
    NSMutableAttributedString attributedStr = new NSMutableAttributedString("How ?\nSome text \nWho? \n Some text");
    attributedStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20), new NSRange(0, 5));
    attributedStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20), new NSRange(16, 5));
    textView.AttributedText = attributedStr;
    
    View.AddSubview(textView);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, June 25, 2020 7:17 AM

All replies

  • User369979 posted

    We could use AttributedText to add different attributes to a string:

    UITextView textView = new UITextView(new CGRect(0, 100, 100, 200));
    
    NSMutableAttributedString attributedStr = new NSMutableAttributedString("How ?\nSome text");
    attributedStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20), new NSRange(0, 5));
    textView.AttributedText = attributedStr;
    
    View.AddSubview(textView);
    

    Here is the effect: Or try to use a UILabel to indicate a title:

    UILabel titleLabel = new UILabel(new CGRect(0, 100, 100, 44));
    titleLabel.AttributedText = new NSAttributedString("How ?", NSDictionary.FromObjectAndKey(UIFont.BoldSystemFontOfSize(20), UIStringAttributeKey.Font));
    
    // Add content
    
    Thursday, June 25, 2020 6:17 AM
  • User389827 posted

    Thank you for you help.

    I have implemented the same . In this string I need bold only the text that ends with ?.

    How ? is bold same like that Who? need to be in bold. Im handling the same text is multiple language and using uitextview is the only option for me !!.

    UITextView textView = new UITextView(new CGRect(0, 100, 100, 200));

            NSMutableAttributedString attributedStr = new NSMutableAttributedString("How ?\nSome text \n Who? \n Some text");
            attributedStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20), new NSRange(0, 5));
            textView.AttributedText = attributedStr;
    
            View.AddSubview(textView);
    
    Thursday, June 25, 2020 6:49 AM
  • User369979 posted

    Add another attribute for the "Who?"

    UITextView textView = new UITextView(new CGRect(0, 100, 100, 200));
    
    NSMutableAttributedString attributedStr = new NSMutableAttributedString("How ?\nSome text \nWho? \n Some text");
    attributedStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20), new NSRange(0, 5));
    attributedStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(20), new NSRange(16, 5));
    textView.AttributedText = attributedStr;
    
    View.AddSubview(textView);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, June 25, 2020 7:17 AM