iOS7 之前,如下:
Objective-C
NSString *str = @"Hello World"
[str drawInRect: CGRectMake(10,10,300, 30)
withFont:[UIFont fontWithName: @"Courier" size: kCellFontSize]
lineBreakMode: NSLineBreakByTruncatingTail
alignment: NSTextAlignmentRight
]
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];