- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试计算 uitableview 单元格中文本标签的高度。在看到 sizewithfont 在 ios 7 中被弃用后,我实现了 sizewithattributes,但返回值远小于标签所包含的文本大小正确的值。我也试过 sizetofit 方法也无济于事。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *message = self.messages[indexPath.row];
UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:3];
UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:2];
messageContent.text = [message objectForKey:@"messageContent"];
NSString *content = [message objectForKey:@"messageContent"];
NSLog(@"Message: %@", content);
CGSize textSize = [content sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]}];
messageContent.font = [UIFont fontWithName:@"HelveticaNue-Light" size:17.0];
CGRect messageFrame = messageContent.frame;
messageFrame.size = textSize;
messageContent.frame = messageFrame;
nameLabel.text = [message objectForKey:@"senderName"];
NSString *senderPicture = [message objectForKey:@"senderPicture"];
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: senderPicture]]];
image.image = myImage;
image.layer.cornerRadius = 27.0;
image.layer.masksToBounds = YES;
//Configure the cell...
return cell;
}
最佳答案
user/Elio.d有一个很好的答案here.
我在下面附上了他的回答的抄本。如果对你有帮助请务必去发Elio.d对 his original answer 点赞
你可以试试这个:
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0f]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
关于ios - sizewithattributes 未返回正确的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20260384/
这个问题在这里已经有了答案: Crash in iOS 7.0.3 for NSMutableAttributedString [duplicate] (1 个回答) 关闭 9 年前。 我的字典的一
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-top
我正在尝试计算 uitableview 单元格中文本标签的高度。在看到 sizewithfont 在 ios 7 中被弃用后,我实现了 sizewithattributes,但返回值远小于标签所包含的
好的,我正在使用 sizeWithFont: constrainedToSize 功能,但我收到了编译器警告。我读到我需要改用 sizeWithAttributes(链接在这里:Replacement
我正在使用 NSString 的 sizeWithAttributes 方法来获取 NSString 的动态高度。 我的字符串是@"This is address line1\n This is ad
iOS 7 使用 NSString::sizeWithAttributes 计算给定 NSDictionary 属性的 NSString 边界框的大小。但是,我找不到列出可在该 NSDictionar
如何获得 NSString 的大小,就好像它在 NSRect 中绘制一样。问题是当我尝试 -[NSString sizeWithAttributes:] 时,它返回一个 NSSize ,就好像它具有无
一些背景知识:我正在编写一个应用程序,其中包含一个 slider 来更改整个应用程序中使用的字体大小。当用户滑动 slider 时,用户会看到应用程序的字体实时变大和变小。我的很多 UI 都依赖于 N
在 iOS 7 中 sizeWithFont: 已弃用。建议的替换方法是 sizeWithAttributes: 但是当我将方法从 sizeWithFont: 更改为 sizeWithAttribut
这个问题在这里已经有了答案: iOS 7 sizeWithAttributes: replacement for sizeWithFont:constrainedToSize (9 个回答) 关闭
为了计算 UITextView 的 intrinsicContentSize,我想知道如何获取属性字符串的所有属性,以便将它们传递给 >-[NSString sizeWithAttributes:]?
我正在尝试使用 sizewithattributes 方法计算 uitableview 中 uilabels 的正确高度。但是它返回的高度远低于应有的高度,标签文本被截断并且从 textwithatt
在 objective-C 中我能够使用: CGSize stringsize = [strLocalTelefone sizeWithAttributes:@{NSFontAttr
我有一个 UISlider 可以更改 UITextView 的字体大小。在更改字体大小之前,我想检查一下 UITextView 的高度是否低于其 View 容器的高度。问题是 sizeToFit 和
我有一个从 NSDictionary 返回的 NSValue。 NSValue 是使用 sizeWithAttributes 创建的。我想知道如何将 NSValue 恢复为我可以使用它来创建标签大小的
这个问题在这里已经有了答案: Xcode 6.1 Attribute Dictionaries in Swift (1 个回答) 关闭 8 年前。 我的代码在 iOS 8.0 上运行正常,自从我更新
下面这段代码在 Xcode 6 beta 5 中运行良好: func fitText(){ let size = (self.text as NSString).sizeWithAttribu
我正在开发一款 Mac 应用程序,可以让用户放大内容。我需要测量我呈现的文本的大小。当我缩放字体以进行缩放时,字体的点大小非常小(即 0.001)。当我使用 [NSString sizeWithAtt
如何获得CGSize NSString 的值在 iOS 7 SDK 中,只需将以下代码行转换为 sizeWithAttributes . CGSize size = [text sizeWithFon
如何从新的 iOS 7 方法 sizeWithAttributes 返回多行文本 CGSize? 我希望它产生与 sizeWithFont:constrainedToSize 相同的结果。 NSStr
我是一名优秀的程序员,十分优秀!