Hello guys, today we learn about “Text Formatting In Swift”.
OverView Of Text Formatting:-
In this blog, we are going to learn about how to format the text like:- bold, italic, underline, and how can we change the text color. sometimes we need to bold, italic, or underline some word in a paragraph. so in this blog, we learn this thing one by one.
For doing text formatting we need NSAttributedString
What is NSAttributedString?
NSAttributedString uses with UILabel and UITextView which is accepted attribute string directly. when we write a simple string we don’t need it but sometimes we want some string characters like – bold, italic, underline and sometimes we want to change the color of some character that time we called a class knows as NSAtrributedString.
Uses:-
1. Bold:- for changing the text as bold we need .boldSystemFont
1 |
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18) |
2. Italic:- for changing the text as italic we need .italicSyatemFont
1 |
NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 18.0) |
3. Underline:- for changing the text as underline we need NSUnderlineStyle
1 |
[NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue] |
4. color:- for changing the text color we need .foregroundColor: color name
1 |
[NSAttributedString.Key.foregroundColor: UIColor.red] |
Full Example:-
- create a UIScreen
2. code for text formatting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import UIKit class ViewController: UIViewController { @IBOutlet weak var btnOutlet: UIButton! @IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLoad() } @IBAction func btnTapp(_ sender: Any) { if let text = textView { let range = text.selectedRange let string = NSMutableAttributedString(attributedString: textView.attributedText) let boldAttribute = [ NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18) ] string.addAttributes(boldAttribute, range: textView.selectedRange) textView.attributedText = string textView.selectedRange = range } } @IBAction func italicTapp(_ sender: Any) { if let text = textView { let range = text.selectedRange let string = NSMutableAttributedString(attributedString: textView.attributedText) let italicAttribute = [ NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 18.0) ] string.addAttributes(italicAttribute, range: textView.selectedRange) textView.attributedText = string textView.selectedRange = range } } @IBAction func underlineTapp(_ sender: Any) { if let text = textView { let range = text.selectedRange let string = NSMutableAttributedString(attributedString: textView.attributedText) let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue] string.addAttributes(underlineAttribute, range: textView.selectedRange) textView.attributedText = string textView.selectedRange = range } } @IBAction func colourTapp(_ sender: Any) { if let text = textView { let range = text.selectedRange let string = NSMutableAttributedString(attributedString: textView.attributedText) let colorAttribute = [NSAttributedString.Key.foregroundColor: UIColor.red] string.addAttributes(colorAttribute, range: textView.selectedRange) textView.attributedText = string textView.selectedRange = range } } } |
Output:-
Conclusion:-
In this blog, we discussed Text Formatting In Swift
I hope this blog will help you to get about text formatting
Thanks for reading !!