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 - 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
Lazy Loading Properties in Swift
Today Extension Failed to Inherit Coremedia Permissions From
Cannot Load Underlying Module for Xctest
What Is a 'Non-Nominal Type' in Swift
How to Display Realm Results in Swiftui List
What Is the Advantage of a Lazy Var in Swift
Extend Existing Protocols to Implement Another Protocol with Default Implements
How to Conform to Nscopying and Implement Copywithzone in Swift 2
In Swift, How to Have a Uiscrollview Subclass That Has an Internal and External Delegate
How to Improve Camera Quality in Arkit
Obj-C Cocoapods + Swift Framework
Exponentiation Operator in Swift
Swift's JSONdecoder with Multiple Date Formats in a JSON String
Prefer Large Titles and Refreshcontrol Not Working Well