8

テーブルビューセルの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;

\n日付と共有の属性文字列の間に追加すると、結果は良好です。

しかし、私は同じ行に2つの文字列を持ちたい..

アイデア ?

ありがとう

4

3 に答える 3

1

これを試して

日付の後に 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;
于 2014-03-24T19:32:46.010 に答える
0

iOS では使用できない NSTextTable を間接的に使用することができます。

次のような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 が表示されます。

于 2017-07-21T08:58:49.447 に答える