gpt4 book ai didi

ios - 使用 drawInRect 对齐文本 :withAttributes:

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:24 24 4
gpt4 key购买 nike

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

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

我正在为 iOS 7 升级。以上方法已弃用。我现在正在使用 drawInRect:withAttributes:attributes 参数是一个 NSDictionary 对象。我可以使用以下方法让 drawInRect:withAttributes: 为以前的 font 参数工作:

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

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

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

我应该将哪些键值对添加到字典以获取NSLineBreakByTruncatingTailNSTextAlignmentRight

最佳答案

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

来自 docs :

NSParagraphStyleAttributeName

The value of this attribute is an NSParagraphStyle object. 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 defaultParagraphStyle method of 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];

关于ios - 使用 drawInRect 对齐文本 :withAttributes:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com