ios 使用 drawInRect:withAttributes 对齐文本:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18948180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:33:16  来源:igfitidea点击:

align text using drawInRect:withAttributes:

iosnsstringios7

提问by StephenAshley.developer

In the iOS 5 version of my app I had:

在我的应用程序的 iOS 5 版本中,我有:

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

I'm upgrading for iOS 7. The above method is deprecated. I'm now using drawInRect:withAttributes:. The attributesparameter is an NSDictionary object. I can get drawInRect:withAttributes:to work for the former fontparameter using this:

我正在升级 iOS 7。不推荐使用上述方法。我现在使用drawInRect:withAttributes:。的属性参数是一个NSDictionary对象。我可以使用以下命令让drawInRect:withAttributes:为以前的字体参数工作:

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

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

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

What key-value pairs do I add to dictionaryto get NSLineBreakByTruncatingTailand NSTextAlignmentRight?

我将哪些键值对添加到字典以获得NSLineBreakByTruncatingTailNSTextAlignmentRight

回答by Hejazi

There is one key to set the paragraph style of the text (including line breaking mode, text alignment, and more).

一键设置文本的段落样式(包括换行模式、文​​本对齐方式等)。

From docs:

文档

NSParagraphStyleAttributeName

The value of this attribute is an NSParagraphStyleobject. Use this attribute to apply multiple attributes to a range of text. If you do not specify this attribute, the string uses the default paragraph attributes, as returned by the defaultParagraphStylemethod of NSParagraphStyle.

NSParagraphStyleAttributeName

该属性的值是一个NSParagraphStyle对象。使用此属性可将多个属性应用于一系列文本。如果不指定此属性,则字符串使用默认段落属性,如 的defaultParagraphStyle方法返回的那样NSParagraphStyle

So, you can try the following:

因此,您可以尝试以下操作:

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];

回答by Darius Miliauskas

The code is going that way:

代码是这样的:

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];