I have to show 3 text on one UITextView
, This can be multiline or single line,
- in Multiline there is no issue .
but when there is single line enabled then text is truncated from end because i'm using
.lineBreakMode = NSLineBreakMode.byTruncatingTail
3. But my desired work it to truncate the middle part of Text (Message)
what i have tried till now !
@IBAction func btnSwitchAction(_ sender: UISwitch) {
self.isMultilineEnabled = sender.isOn
}
func setup() {
textView.isScrollEnabled = false
textView.isEditable = false
reloadTextView()
}
func reloadTextView() {
let padding = textView.textContainer.lineFragmentPadding
textView.textContainerInset = UIEdgeInsets(top: 0, left: -padding, bottom: 0, right: -padding)
textView.textContainer.maximumNumberOfLines = isMultilineEnabled ? 0 : 1;
textView.textContainer.lineBreakMode = isMultilineEnabled ? NSLineBreakMode.byWordWrapping : NSLineBreakMode.byTruncatingTail
textView.attributedText = getAttributedString(title: "Des", messageStr: "In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder", actionButton: "Share")
textView.layoutIfNeeded()
textView.translatesAutoresizingMaskIntoConstraints = true
textView.sizeToFit()
}
private func getAttributedString(title: String, messageStr: String, actionButton: String = "") -> NSAttributedString {
let defaultFont = UIFont.systemFont(ofSize: fontSize)
let boldFont = UIFont.systemFont(ofSize: fontSize, weight: .bold)
let finalAttributedString = NSMutableAttributedString.init(string: "")
let firstText = "\(title):"
let regulerAttributedText = NSMutableAttributedString(string: firstText,
attributes: [NSAttributedString.Key.font : boldFont])
finalAttributedString.append(regulerAttributedText)
let secondAttributedText = NSMutableAttributedString(string: messageStr,
attributes: [NSAttributedString.Key.font : defaultFont])
finalAttributedString.append(secondAttributedText)
finalAttributedString.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.black.cgColor], range: NSMakeRange(0, finalAttributedString.string.count))
if !actionButton.isEmpty {
let actionFont = UIFont.systemFont(ofSize: fontSize, weight: .medium)
let actionTextAttribute = NSMutableAttributedString(string: " \(actionButton)")
actionTextAttribute.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange.cgColor,
NSAttributedString.Key.font : defaultFont],
range: NSMakeRange(0, actionButton.count + 1))
finalAttributedString.append(actionTextAttribute)
}
return finalAttributedString
}
If i can get the number of text which can fit in one line then i can archive the desired result by trimming middle text . i wrote extension but its not giving correct number of texts.
extension NSLayoutManager {
/**
Returns characters range that completely fits into container.
*/
public func characterRangeThatFits(textContainer container: NSTextContainer) -> NSRange {
var rangeThatFits = self.glyphRange(for: container)
rangeThatFits = self.characterRange(forGlyphRange: rangeThatFits, actualGlyphRange: nil)
return rangeThatFits
}
}