首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sizewithfont:forwidth:换行模式

sizewithfont:forwidth:换行模式
EN

Stack Overflow用户
提问于 2012-09-05 10:39:37
回答 3查看 983关注 0票数 3

为什么这个不起作用?无论字符串的长度如何,它总是返回18。有this thread,但没有一个明确的答案。

代码语言:javascript
复制
    NSString * t = @"<insert super super long string here>";

    CGSize size = [t sizeWithFont:[UIFont systemFontOfSize:14.0] forWidth:285 lineBreakMode:UILineBreakModeWordWrap];

    NSLog(@"size.height is %f and text is %@", size.height, t);

谢谢,

托德

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-05 11:03:43

请改用sizeWithFont:constrainedToSize:lineBreakMode:

代码语言:javascript
复制
NSString * t = @"<insert super super long string here>";
CGSize constrainSize = CGSizeMake(285, MAXFLOAT);
CGSize size = [t sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:constrainSize lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"size.height is %f and text is %@", size.height, t);
票数 5
EN

Stack Overflow用户

发布于 2015-11-30 14:17:52

不推荐使用的方法: NS_DEPRECATED_IOS(2_0,7_0)

代码语言:javascript
复制
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");

示例

代码语言:javascript
复制
CGSize titleTextSize = [self.titleLabel.text sizeWithFont:self.myLabel.font forWidth:myLabelWidth lineBreakMode:NSLineBreakByTruncatingTail];

新方法

使用:

代码语言:javascript
复制
- (CGRect)boundingRectWithSize:(CGSize)size
                       options:(NSStringDrawingOptions)options
                    attributes:(NSDictionary<NSString *,
                                        id> *)attributes
                       context:(NSStringDrawingContext *)context

示例:

代码语言:javascript
复制
 // Create a paragraph style with the desired line break mode
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;

    // Create the attributes dictionary with the font and paragraph style
    NSDictionary *attributes = @{
                                 NSFontAttributeName:self.myLabel.font,
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };

    // Call boundingRectWithSize:options:attributes:context for the string
    CGRect textRect = [self.countLabel.text boundingRectWithSize:CGSizeMake(widthOfMyLabel, 999999.0f)
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:attributes
                                           context:nil];

See Appple Doc

票数 1
EN

Stack Overflow用户

发布于 2012-09-05 15:56:14

代码语言:javascript
复制
CGSize size = [t sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:16.0] constrainedToSize:CGSizeMake(220,500) lineBreakMode:UILineBreakModeWordWrap];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12273804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档