Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift NSAttributedString Trim

Tags:

I want to get ride of the white spaces in front and at the end of my NSAttributedString(Trimming it). I can't simply convert it to string and do trimming because there are images(attachments) in it. How can i do it?

like image 862
Tommy Wei Avatar asked Aug 03 '16 08:08

Tommy Wei


People also ask

How do you trim leading and trailing spaces in Swift?

To remove leading and trailing spaces, we use the trimmingCharacters(in:) method that removes all characters in provided character set. In our case, it removes all trailing and leading whitespaces, and new lines.

How do I remove white space in Swift?

Swift String trimmingCharacters() The trimmingCharacters() method removes whitespace from both ends of a string.


2 Answers

Create extension of NSAttributedString as below.

extension NSAttributedString {      public func attributedStringByTrimmingCharacterSet(charSet: CharacterSet) -> NSAttributedString {          let modifiedString = NSMutableAttributedString(attributedString: self)         modifiedString.trimCharactersInSet(charSet: charSet)          return NSAttributedString(attributedString: modifiedString)      } }  extension NSMutableAttributedString {      public func trimCharactersInSet(charSet: CharacterSet) {         var range = (string as NSString).rangeOfCharacter(from: charSet as CharacterSet)           // Trim leading characters from character set.          while range.length != 0 && range.location == 0 {             replaceCharacters(in: range, with: "")             range = (string as NSString).rangeOfCharacter(from: charSet)          }           // Trim trailing characters from character set.         range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards)          while range.length != 0 && NSMaxRange(range) == length {             replaceCharacters(in: range, with: "")             range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards)          }      } } 

and use in viewController where you want to use. like this

let attstring = NSAttributedString(string: "this is test message. Please wait.                   ") let result = attstring.attributedStringByTrimmingCharacterSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) 
like image 155
Bhautik Ziniya Avatar answered Sep 18 '22 14:09

Bhautik Ziniya


This works even with emoji in the text

extension NSAttributedString {      /** Will Trim space and new line from start and end of the text */     public func trimWhiteSpace() -> NSAttributedString {         let invertedSet = CharacterSet.whitespacesAndNewlines.inverted         let startRange = string.utf16.description.rangeOfCharacter(from: invertedSet)         let endRange = string.utf16.description.rangeOfCharacter(from: invertedSet, options: .backwards)         guard let startLocation = startRange?.upperBound, let endLocation = endRange?.lowerBound else {             return NSAttributedString(string: string)         }          let location = string.utf16.distance(from: string.startIndex, to: startLocation) - 1         let length = string.utf16.distance(from: startLocation, to: endLocation) + 2         let range = NSRange(location: location, length: length)         return attributedSubstring(from: range)     }  } 

USAGE

let attributeString = NSAttributedString(string: "\n\n\n     Hi ๐Ÿ‘‹ ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ง๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘จโ€๐Ÿ‘ฆ buddy.              ") let result = attributeString.trimWhiteSpace().string // Hi ๐Ÿ‘‹ ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ง๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง๐Ÿ‘จโ€๐Ÿ‘ฆ buddy. 
like image 36
kamalraj venkatesan Avatar answered Sep 21 '22 14:09

kamalraj venkatesan