2014-03-24 85 views
3

我想写在我的tableview单元格detailTextLabel的同一行上。IOS在同一行上多个左右对齐

例如 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; 

如果我添加日期和共享属性字符串之间的\n,效果良好。

,但我想对同一线上的两个字符串..

的想法?

感谢

+2

你可以有两个标签具有相同的性质和不同textalignment左右 –

+0

好了,但有很多原因,我想写出我的琴弦在detailTextLabel – user3433920

+0

你添加\ n是怎么做的,什么是你的descriptionString和finaldate –

回答

2

试试这个

增加了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; 
+0

不受约束。 NSRangeException – user3433920

+0

编辑答案现在尝试 –

+0

它完美的作品,谢谢! – user3433920

-1

这是可能的间接使用NSTextTable这是iOS上不可用。

如果您创建HTML表所示:

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

然后将其转换到像这样的属性串:

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

你会得到你所需要的东西,如果你检查段落属性在那个属性字符串中 - 你会在那里看到NSTextTable。

相关问题