2014-03-24 53 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。

相關問題