I want to write on the same line of my tableview cell detailTextLabel.
For example
Left string right string
.
I\'m doing this :
It's possible to indirectly use NSTextTable which is unavailable on iOS.
If you create html table like:
<table width="100%">
<tr>
<td align="left">Left string</td>
<td align="right">Right string</td>
</tr>
</table>
And then convert it to an attributed string like that way:
extension NSAttributedString {
convenience init?(html: String) {
guard let data = html.data(using: .utf8) else {
return nil
}
try? self.init(data: data, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
], documentAttributes: nil)
}
}
You will get exactly what you need and if you check paragraph attributes in that attributed string - you will see NSTextTable there.
It would be better to create a custom cell with 2 labels but if for some reason you still want to have it in a single label then below is a solution. If you know what is the height of the line then you can use paragraphSpacingBefore to do something like in code below. Note that it does not work when UILabel numberOfLines is set to 1 so you must set numberOfLines to for example 0:
let text = "left\nright"
let at = NSMutableAttributedString(string: text)
let p1 = NSMutableParagraphStyle()
let p2 = NSMutableParagraphStyle()
p1.alignment = .left
p2.alignment = .right
p2.paragraphSpacingBefore = -label.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 4))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 4, length: 6))
label.numberOfLines = 0
label.attributedText = at
try this
added a NSKernAttributeName after date
NSMutableAttributedString *descriptionAttribute = [[NSMutableAttributedString alloc] initWithString:@"hello how are you"];
NSMutableAttributedString *dateAttribute = [[NSMutableAttributedString alloc] initWithString:@"\nfinal datetime"];
NSMutableAttributedString *shareAttribute = [[NSMutableAttributedString alloc] initWithString:@"Share"];
NSMutableParagraphStyle *dateStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[dateStyle setAlignment:NSTextAlignmentLeft];
NSMutableParagraphStyle *shareStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[shareStyle setAlignment:NSTextAlignmentRight];
[dateAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, dateAttribute.length)];
[dateAttribute addAttribute:NSParagraphStyleAttributeName value:dateStyle range:NSMakeRange(0, 13)];
[dateAttribute addAttribute:NSKernAttributeName value:@170 range:NSMakeRange(dateAttribute.length-1, 1)];
[shareAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 2)];
[shareAttribute addAttribute:NSParagraphStyleAttributeName value:shareStyle range:NSMakeRange(0, 5)];
[descriptionAttribute appendAttributedString:[dateAttribute mutableCopy]];
[descriptionAttribute appendAttributedString:[shareAttribute mutableCopy]];
theCell.detailTextLabel.attributedText = descriptionAttribute;