Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bold part of string in UITextView swift

Tags:

Here is the text:

@IBOutlet weak var legalText: UITextView! let textToAppend = "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." legalText.text = legalText.text.stringByAppendingString(textToAppend) 

i want to bold "TERMS OF SERVICE" and "PLEASE 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."

i try with use uilabel programmatically in uitextview but did not work :

var termsofservice : UILabel = UILabel() termsofservice.numberOfLines = 0 termsofservice.text = "TERMS OF SERVICE" termsofservice.font = UIFont.boldSystemFontOfSize(20) termsofservice.textAlignment = NSTextAlignment.Left;  var pleasenote : UILabel = UILabel() pleasenote.numberOfLines = 0 pleasenote.text = "PLEASE 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." pleasenote.font = UIFont.boldSystemFontOfSize(20) pleasenote.textAlignment = NSTextAlignment.Left;  let textToAppend = "\(termsofservice)\nLast Updated: May 7, 2015\n\n\(pleasenote)" 

also try with these but did not work, it only show "TERMS OF SERVICE" and "last update..", did not show "PLEASE NOTE..."

var termsofservice = "TERMS OF SERVICE" var normalText = "\n\nLast Updated: May 7, 2015" var pleasenote  = "\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."  var attributedString = NSMutableAttributedString(string:normalText)  var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)] var boldString = NSMutableAttributedString(string:pleasenote, attributes:attrs) var boldString0 = NSMutableAttributedString(string:termsofservice, attributes:attrs)  boldString0.appendAttributedString(attributedString) attributedString.appendAttributedString(boldString) legalText.attributedText = boldString0 

How to bold that part of string?

Note:the text is still long and have more part of string to bold.

like image 684
Sarimin Avatar asked Jul 27 '15 07:07

Sarimin


People also ask

How do you bold a specific word in a string in Swift?

Convert html to string and font change as per your requirement. func MONTSERRAT_BOLD(_ size: CGFloat) -> UIFont { return UIFont(name: "MONTSERRAT-BOLD", size: size)! }

How do I make my text bold on Uilabel?

Double Click on Bold to select it, and then right click on it to see more options. Select font > Bold from that option. It should do the task.

What is attributed text in Swift?

A value type for a string with associated attributes for portions of its text.


2 Answers

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() 
like image 58
Hamza Ansari Avatar answered Oct 12 '22 16:10

Hamza Ansari


Better still, you can go ahead and use this 1 function in your armoury, in a file like Constants.swift and then you can embolden words within any string, on numerous occasions by calling just ONE LINE of code:

To go in a file without a class, like constants.swift in my case:

import Foundation import UIKit  func addBoldText(fullString: NSString, boldPartOfString: NSString, font: UIFont!, boldFont: UIFont!) -> NSAttributedString {     let nonBoldFontAttribute = [NSAttributedString.Key.font:font!]     let boldFontAttribute = [NSAttributedString.Key.font:boldFont!]     let boldString = NSMutableAttributedString(string: fullString as String, attributes:nonBoldFontAttribute)     boldString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartOfString as String))     return boldString } 

Then you can just call this one line of code for any UILabel:

let normalFont = UIFont(name: "INSERT FONT NAME", size: 14) let boldFont = UIFont(name: "INSERT BOLD FONT NAME", size: 14) self.UILabel.attributedText = addBoldText("Check again in 30 DAYS to find more friends", boldPartOfString: "30 DAYS", font: normalFont!, boldFont: boldFont!) 
like image 30
David West Avatar answered Oct 12 '22 15:10

David West