IOS多个左右alignment在同一行上

我想写在我的tableview单元格detailTextLabel的同一行。

例如Left string right string

我正在这样做:

 NSMutableAttributedString *descriptionAttribute = [[NSMutableAttributedString alloc] initWithString:descriptionString]; NSMutableAttributedString *dateAttribute = [[NSMutableAttributedString alloc] initWithString:finalDate]; 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, 13)]; [dateAttribute addAttribute:NSParagraphStyleAttributeName value:dateStyle range:NSMakeRange(0, 13)]; [shareAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 8)]; [shareAttribute addAttribute:NSParagraphStyleAttributeName value:shareStyle range:NSMakeRange(0, 5)]; [descriptionAttribute appendAttributedString:[dateAttribute mutableCopy]]; [descriptionAttribute appendAttributedString:[shareAttribute mutableCopy]]; myTableViewcell.detailTextLabel.attributedText = descriptionAttribute; 

如果我在date和共享属性string之间添加\n ,结果是好的。

但我想在同一行上有两个string..

一个主意 ?

谢谢

尝试这个

date后添加一个NSKernAttributeName

 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; 

可以间接使用iOS上不可用的NSTextTable。

如果你创build如下的html表格:

 <table width="100%"> <tr> <td align="left">Left string</td> <td align="right">Right string</td> </tr> </table> 

然后将其转换为像这样的属性string:

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

你将得到你所需要的,如果你检查属性string中的段落属性 – 你会在那里看到NSTextTable。

Interesting Posts