Fix posts with multiple <p> tags being handled incorrectly
This commit is contained in:
parent
9b07dd98bf
commit
481f493a9c
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue