Bold Part of String in Uitextview Swift

bold part of string in UITextView swift

Updated for Swift 3

func attributedText() -> NSAttributedString {

let string = "TERMS OF SERVICE\nLast Updated: May 7, 2015\n\nPLEASE NOTE: The HIGO Terms of Service as stated below is effective as of the 'Last Updated' date above for any user who is browsing the HIGO website, or for any user who creates a HIGO account on or after that date." as NSString

let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])

let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]

// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "TERMS OF SERVICE"))
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "PLEASE NOTE:"))

// 4
return attributedString
}

And set attributext text to your text view as:

legalText.attributedText = attributedText()

How to bold some words in my UITextView using NSMutableAttributedString?

You can also set it the following way if you want by setting a dictionary as a whole, as attribute

NSString *strTextView = @"This is some demo Text to set BOLD";

NSRange rangeBold = [strTextView rangeOfString:@"BOLD"];

UIFont *fontText = [UIFont boldSystemFontOfSize:10];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:strTextView];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];

[textViewTermsPolicy setAttributedText:mutAttrTextViewString];

Bold & Non-Bold Text In uitextView - iOS

You can use NSAttributedString to custom the string format:

public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.

var textV = new UITextView(new CGRect(100, 100, 300, 300));

var myMutableString = new NSMutableAttributedString("How ? \n Some text \n Who ? \n Some text");

var range1 = myMutableString.MutableString.LocalizedStandardRangeOfString(new NSString("How ?"));
var range2 = myMutableString.MutableString.LocalizedStandardRangeOfString(new NSString("Who ?"));

var range3 = myMutableString.MutableString.LocalizedStandardRangeOfString(new NSString("Some text"));

myMutableString.AddAttribute(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(24), range1);
myMutableString.AddAttribute(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(24), range2);

myMutableString.AddAttribute(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(14), range3);

textV.AttributedText = myMutableString;
View.AddSubview(textV);
}

Here is the result:

Swift Sample Image 30

Swift - search strings in TextView and make them bold

You can create a textView extension that can be called on your textView outlet:

extension UITextView {    
func makeBold(originalText: String, boldText: String) {

let attributedOriginalText = NSMutableAttributedString(string: originalText)
let boldRange = attributedOriginalText.mutableString.range(of: boldText)

attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 13), range: boldRange)
self.attributedText = attributedOriginalText
}
}

How to use:

@IBOutlet weak var textView: UITextView!

textView.makeBold(originalText: "Make bold", boldText: "bold")


Related Topics



Leave a reply



Submit