Ste*_*per 46 nsstring ios ios7
在我的应用程序的iOS 5版本中,我有:
[self.text drawInRect: stringRect
withFont: [UIFont fontWithName: @"Courier" size: kCellFontSize]
lineBreakMode: NSLineBreakByTruncatingTail
alignment: NSTextAlignmentRight];
Run Code Online (Sandbox Code Playgroud)
我正在升级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];
Run Code Online (Sandbox Code Playgroud)
我将哪些键值对添加到字典中以获取NSLineBreakByTruncatingTail和NSTextAlignmentRight?
Hej*_*azi 140
有一个键可以设置文本的段落样式(包括换行模式,文本对齐等).
来自docs:
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];
Run Code Online (Sandbox Code Playgroud)
代码就是这样:
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];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33316 次 |
最近记录: |