2

I have heading and content two strings,

here i need heading with font 20 and bold and content with font 15 regular, but i am getting whole text in same font size.

here is the code:

import UIKit

class RefundandCancellationViewController: UIViewController {

@IBOutlet weak var refundTextview: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()

    var heading = "Bills or Taxes once paid through the payment gateway shall not be refunded other then in the following circumstances:"
    var content = "\n \n 1. Multiple times debiting of Consumer Card/Bank Account due to ticnical error excluding Payment Gateway charges would be refunded to the consumer with in 1 week after submitting complaint form. \n \n 2. Consumers account being debited with excess amount in single transaction due to tecnical error will be deducted in next month transaction. \n \n 3. Due to technical error, payment being charged on the consumers Card/Bank Account but the Bill is unsuccessful.


        refundTextview.textColor = UIColor.gray
        refundTextview.text = heading + content

}
}
  • Use `NSAttributedString`. `NSAttributedString.Key.Font` should be one you are looking for. – Larme Nov 20 '19 at 10:06
  • @Larme, can i get code snippet for that –  Nov 20 '19 at 10:09
  • You mean https://stackoverflow.com/questions/18365631/example-of-nsattributedstring-with-two-different-font-sizes ? "NSAttributedString + Two + Fonts" in your favorite Search Engine? – Larme Nov 20 '19 at 10:10

1 Answers1

3

You can do that using attributedText.

try like this

var heading = "Bills or Taxes once paid through the payment gateway shall not be refunded other then in the following circumstances:"
var content = "\n \n 1. Multiple times debiting of Consumer Card/Bank Account due to ticnical error excluding Payment Gateway charges would be refunded to the consumer with in 1 week after submitting complaint form. \n \n 2. Consumers account being debited with excess amount in single transaction due to tecnical error will be deducted in next month transaction. \n \n 3. Due to technical error, payment being charged on the consumers Card/Bank Account but the Bill is unsuccessful."

let attributedText = NSMutableAttributedString(string: heading, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20)])

attributedText.append(NSAttributedString(string: content, attributes: [NSAttributedStringKey.font: UIFont.SystemFont(ofSize: 15), NSAttributedStringKey.foregroundColor: UIColor.blue]))

refundTextview.attributedText = attributedText

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
  • Don't use `range(of:)`. That's bad practice since you could have: `heading = "Hello there!"; content = "here"`, you'll have an issue. Instead, create two NSAttributedString with `heading` and then with `content`, and concat them (using a `NSMutableAttributedString`). – Larme Nov 20 '19 at 10:16
  • thank you, but how to make heading BOLD and content regular, and now i am unable to give color to text its coming in black color –  Nov 20 '19 at 10:26
  • can we give "Helvetica Neue" font –  Nov 20 '19 at 10:40
  • 1
    yes, try like this `UIFont(name: "HelveticaNeue-Bold", size: 20)!, NSAttributedStringKey.foregroundColor: UIColor.green]` – Jakir Hossain Nov 20 '19 at 10:52