From 481f493a9c4036632b0db20ec44c450d687dc20e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 29 Aug 2018 21:51:04 -0400 Subject: [PATCH] Fix posts with multiple

tags being handled incorrectly --- Tusker/Extensions/AttributedString+Trim.swift | 28 +++++++++++++++++++ Tusker/Views/HTMLContentLabel.swift | 3 ++ 2 files changed, 31 insertions(+) create mode 100644 Tusker/Extensions/AttributedString+Trim.swift diff --git a/Tusker/Extensions/AttributedString+Trim.swift b/Tusker/Extensions/AttributedString+Trim.swift new file mode 100644 index 00000000..60ddeea2 --- /dev/null +++ b/Tusker/Extensions/AttributedString+Trim.swift @@ -0,0 +1,28 @@ +// +// AttributedString+Trim.swift +// Tusker +// +// Created by Shadowfacts on 8/29/18. +// Copyright © 2018 Shadowfacts. All rights reserved. +// + +import Foundation + +extension NSMutableAttributedString { + + func trimCharactersInSet(_ charSet: CharacterSet) { + var range = (string as NSString).rangeOfCharacter(from: charSet) + + while range.length != 0 && range.location == 0 { + replaceCharacters(in: range, with: "") + range = (string as NSString).rangeOfCharacter(from: charSet) + } + + range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards) + while range.length != 0 && range.length + range.location == length { + replaceCharacters(in: range, with: "") + range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards) + } + } + +} diff --git a/Tusker/Views/HTMLContentLabel.swift b/Tusker/Views/HTMLContentLabel.swift index 7e93adcb..507688b8 100644 --- a/Tusker/Views/HTMLContentLabel.swift +++ b/Tusker/Views/HTMLContentLabel.swift @@ -119,6 +119,7 @@ class HTMLContentLabel: UILabel { let (attributedText, links) = attributedTextForHTMLNode(body) self.links = links let mutAttrString = NSMutableAttributedString(attributedString: attributedText) + mutAttrString.trimCharactersInSet(.whitespacesAndNewlines) mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length)) self.attributedText = mutAttrString } @@ -153,6 +154,8 @@ class HTMLContentLabel: UILabel { links[linkRange] = url } + case "p": + attributed.append(NSAttributedString(string: "\n\n")) default: break }