47

私のアプリのiOS 5バージョンでは、次のことがありました。

[self.text drawInRect: stringRect
             withFont: [UIFont fontWithName: @"Courier" size: kCellFontSize]
        lineBreakMode: NSLineBreakByTruncatingTail
            alignment: NSTextAlignmentRight];

iOS 7 にアップグレードしています。上記の方法は非推奨です。私は現在drawInRect:withAttributes:を使用しています。attributesパラメータはNSDictionary オブジェクトです。これを使用して、 drawInRect:withAttributes:を以前のフォントパラメータで機能させることができます。

      UIFont *font = [UIFont fontWithName: @"Courier" size: kCellFontSize];

      NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
                                  nil];

      [self.text drawInRect: stringRect
             withAttributes: dictionary];

NSLineBreakByTruncatingTailNSTextAlignmentRightを取得するために辞書に追加するキーと値のペアは何ですか?

4

2 に答える 2

141

テキストの段落スタイルを設定するためのキーが 1 つあります (改行モード、テキストの配置などを含む)。

ドキュメントから:

NSParagraphStyleAttributeName

この属性の値はNSParagraphStyleオブジェクトです。この属性を使用して、複数の属性をテキストの範囲に適用します。この属性を指定しない場合、文字列は のdefaultParagraphStyleメソッドによって返されるデフォルトの段落属性を使用しますNSParagraphStyle

したがって、次のことを試すことができます。

UIFont *font = [UIFont fontWithName:@"Courier" size:kCellFontSize];

/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentRight;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                    NSParagraphStyleAttributeName: paragraphStyle };

[text drawInRect:rect withAttributes:attributes];
于 2013-09-22T23:54:36.040 に答える
5

コードは次のようになります。

CGRect textRect = CGRectMake(x, y, length-x, maxFontSize);
UIFont *font = [UIFont fontWithName:@"Courier" size:maxFontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;


   paragraphStyle.alignment = NSTextAlignmentRight;
    NSDictionary *attributes = @{ NSFontAttributeName: font,
                                  NSParagraphStyleAttributeName: paragraphStyle,
                                  NSForegroundColorAttributeName: [UIColor whiteColor]};
[text drawInRect:textRect withAttributes:attributes];
于 2016-03-05T14:32:15.813 に答える