Fix posts with multiple <p> tags being handled incorrectly

This commit is contained in:
Shadowfacts 2018-08-29 21:51:04 -04:00
parent bea738e0ec
commit c155f695f5
2 changed files with 31 additions and 0 deletions

View File

@ -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)
}
}
}

View File

@ -119,6 +119,7 @@ class HTMLContentLabel: UILabel {
let (attributedText, links) = attributedTextForHTMLNode(body) let (attributedText, links) = attributedTextForHTMLNode(body)
self.links = links self.links = links
let mutAttrString = NSMutableAttributedString(attributedString: attributedText) let mutAttrString = NSMutableAttributedString(attributedString: attributedText)
mutAttrString.trimCharactersInSet(.whitespacesAndNewlines)
mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length)) mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length))
self.attributedText = mutAttrString self.attributedText = mutAttrString
} }
@ -153,6 +154,8 @@ class HTMLContentLabel: UILabel {
links[linkRange] = url links[linkRange] = url
} }
case "p":
attributed.append(NSAttributedString(string: "\n\n"))
default: default:
break break
} }