15

I have a string which has no whitespace at end of the string but when I am converting to NSAttributedString and set to UITextView it was seen some whitespace at end of the UILabel.

For making NSAttributedString I am using following code. In my code expectedLabelSize gives a big height.

UILabel *tempLbl = [[UILabel alloc]init];
tempLbl.font = txtView.font;
tempLbl.text = string;

NSDictionary *dictAttributes = [NSDictionary dictionaryWithObjectsAndKeys: tempLbl.font, NSFontAttributeName, aParaStyle, NSParagraphStyleAttributeName,[UIColor darkGrayColor],NSForegroundColorAttributeName, nil];

CGSize expectedLabelSize = [string boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dictAttributes context: nil].size;
3

4 Answers 4

24

Swift answer but it give you a start if you translate it to Obj-C (or make a swift file just with the extension for use in your Obj-C then)

extension NSMutableAttributedString {

    func trimmedAttributedString(set: CharacterSet) -> NSMutableAttributedString {

        let invertedSet = set.inverted

        var range = (string as NSString).rangeOfCharacter(from: invertedSet)
        let loc = range.length > 0 ? range.location : 0

        range = (string as NSString).rangeOfCharacter(
                            from: invertedSet, options: .backwards)
        let len = (range.length > 0 ? NSMaxRange(range) : string.characters.count) - loc

        let r = self.attributedSubstring(from: NSMakeRange(loc, len))
        return NSMutableAttributedString(attributedString: r)
    }
}

Usage :

let noSpaceAttributedString =
   attributedString.trimmedAttributedString(set: CharacterSet.whitespacesAndNewlines)
2
  • 1
    Thanks Dean, superb. I edited in the minor changes for the latest Swift. Awesome thanks
    – Fattie
    May 16, 2017 at 23:19
  • 1
    This does work very good, except if the white space is in the middle of the text. Any idea on how to fix this ?
    – r3dm4n
    Jul 14, 2018 at 7:57
11

Swift 4 and above

we can create extension on NSMutableAttributedString which returns new NSAttributedString by removing .whitespacesAndNewlines

extension NSMutableAttributedString {

    func trimmedAttributedString() -> NSAttributedString {
        let invertedSet = CharacterSet.whitespacesAndNewlines.inverted
        let startRange = string.rangeOfCharacter(from: invertedSet)
        let endRange = string.rangeOfCharacter(from: invertedSet, options: .backwards)
        guard let startLocation = startRange?.upperBound, let endLocation = endRange?.lowerBound else {
            return NSAttributedString(string: string)
        }
        let location = string.distance(from: string.startIndex, to: startLocation) - 1
        let length = string.distance(from: startLocation, to: endLocation) + 2
        let range = NSRange(location: location, length: length)
        return attributedSubstring(from: range)
    }
}
3
  • 2
    This will crash if the last character in the string is an emoji! Jan 10, 2020 at 12:44
  • I translated this solution to ObjectiveC (with some modifications for my need --- extended NSMutableAttributedString with 'trimWhiteSpace' on self) but I need some explanation on the calculations that hop between the Swift string (startIndex, upperBound etc) and the original NSString and NSRange numbers. could you elaborate just a little? Aug 16, 2021 at 10:46
  • There isn't much sense in extending NSMutableAttributedString without actually allowing to change it. This extension is more suitable for the immutable NSAttributedString - to return a trimmed version of itself. An extension to NSMutableAttributedString should affect the received itself - see my Obj-C extension Aug 16, 2021 at 11:00
2
extension NSAttributedString {
    func trimmedAttributedString() -> NSAttributedString {
        let invertedSet = CharacterSet.whitespacesAndNewlines.inverted
        let startRange = string.rangeOfCharacter(from: invertedSet)
        let endRange = string.rangeOfCharacter(from: invertedSet, options: .backwards)
        guard let startLocation = startRange?.lowerBound, let endLocation = endRange?.lowerBound else {
            return self
        }
        let range = NSRange(startLocation...endLocation, in: string)
        return attributedSubstring(from: range)
    }
}
1

Here's an Obj-C rendering of @suhit Patil's Swift NSAttributedString extension - but this time, it's on NSMutableAttributedString and acts on self. Hope it'll be of use to someone.

@interface NSMutableAttributedString (OITTrimming)

- (void)trimWhiteSpace;

@end

@implementation NSMutableAttributedString (OITTrimming)

 /*!
  @abstract trims whitespacesAndNewlines off the receiver
*/
- (void)trimWhiteSpace {
    NSCharacterSet *legalChars = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
    NSRange startRange = [self.string rangeOfCharacterFromSet: legalChars];
    NSRange endRange = [self.string rangeOfCharacterFromSet: legalChars options:NSBackwardsSearch];
    if (startRange.location == NSNotFound || endRange.location == NSNotFound) {
        // there are no legal characters in self --- it is ALL whitespace, and so we 'trim' everything leaving an empty string
        [self setAttributedString:[NSAttributedString new]];
    }
    else {
        NSUInteger startLocation = NSMaxRange(startRange), endLocation = endRange.location;
        NSRange range = NSMakeRange(startLocation - 1, endLocation - startLocation + 2);
        [self setAttributedString:[self attributedSubstringFromRange:range]];
    }
}

@end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.