在我的应用程序的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?
有没有办法使用以下方法获得正确的NSString大小:
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
Run Code Online (Sandbox Code Playgroud)
不会被2或300个字符串抛出.目前,如果我尝试在这些长字符串上使用此方法,则会错误地计算它们,并且最终会在UITextView的底部显示大量空格.
我尝试过使用UILineBreakModeWordWrap和UILineBreakModeCharacterWrap.
调整大小正在进行中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat result = 44.0f;
NSString* text = nil;
CGFloat width = 0;
CGFloat tableViewWidth;
CGRect bounds = [UIScreen mainScreen].bounds;
tableViewWidth = bounds.size.width;
width = tableViewWidth - 150;
text = stringWithLongWords;
if (text) {
CGSize textSize = { width, 20000.0f };
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:10.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 50.0f;
result = MAX(size.height, 44.0f+30.0f);
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码从字符串长度计算标签的高度.我使用xcode 5.0,它在iOS 6模拟器中工作正常,但它在iOS 7中运行不佳.
NSString* str = [[array objectAtIndex:i]valueForKey:@"comment"];
CGSize size = [className sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
Height_1 = size.height;
Run Code Online (Sandbox Code Playgroud)
如果iOS 7有任何解决方案,那么请帮忙.提前致谢
sizeWithFont:ConstrainedToSize:lineBreakMode:方法在iOS 7中已弃用,我有点不确定如何处理这个问题.经过对互联网的一些研究后,我发现有一种新方法可以解决这个问题,即:
[txt drawWithRect: options: attributes: context:]
Run Code Online (Sandbox Code Playgroud)
这是我目前正在尝试运行的方法:
+ (CGSize)textSizeForText:(NSString *)txt
{
CGFloat width = [UIScreen mainScreen].applicationFrame.size.width * 0.75f;
CGFloat height = MAX([JSBubbleView numberOfLinesForMessage:txt],
[txt numberOfLines]) * [JSMessageInputView textViewLineHeight];
return [txt sizeWithFont:[JSBubbleView font]
constrainedToSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
lineBreakMode:NSLineBreakByWordWrapping];
}
Run Code Online (Sandbox Code Playgroud)
但我很难将其转换为新方法.主要是使用lineBreakMode:在新方法中无处可去.有任何想法吗?
我的代码是
CGSize textSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(self.bounds.size.width - 106, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何解决此警告?