首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sizewithFont和drawinRect格式

sizewithFont和drawinRect格式
EN

Stack Overflow用户
提问于 2017-04-02 04:40:59
回答 1查看 67关注 0票数 0

我有一个iPhone应用程序,每年都会有一些代码元素生成不推荐使用的问题。我的应用程序运行得很好,除了一些小的格式问题。我已经尝试使用建议的代码,但它只会导致错误。我真的很想解决这些问题,看看是否能解决格式问题。有人能帮我拿一下这些吗。

First Issue:'sizeWithFont:constrainedToSize:lineBreakMode:‘被弃用: first在iOS 7.0中被弃用-使用-boundingRectWithSize:options:attributes:context:尝试使用建议的替代,但只是导致了错误(参见下面的代码)。不确定在选项、属性和上下文中适合当前代码的位置。

第二个问题:'drawInRect:withFont:lineBreakMode:alignment:‘被弃用:在iOS 7.0中第一次被弃用-使用-drawInRect:withAttributes:尝试使用建议的替代品,但只是导致了错误(参见下面的代码)。不确定在哪里适合当前的代码re withAttributes。

代码语言:javascript
复制
 //Draw text fo our header.
        CGContextRef    currentContextHeader = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(currentContextHeader, 0.3, 0.7, 0.2, 1.0);

        NSString *textToDrawHeader = [NSString stringWithFormat:@"%@", enterSubject.text];

        UIFont *fontHeader = [UIFont systemFontOfSize:24.0];

        //Original Code that generated the issue
        //CGSize stringSizeHeader = [textToDrawHeader sizeWithFont:fontHeader constrainedToSize:CGSizeMake(_pageSize.width - 2*kBorderInset-2*kMarginInset, _pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:NSLineBreakByWordWrapping];

        //Proposed change that resulted in an error
        CGSize stringSizeHeader = [textToDrawHeader boundingRectWithSize:fontHeader options:attributes:context:constrainedToSize:CGSizeMake(_pageSize.width - 2*kBorderInset-2*kMarginInset, _pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:NSLineBreakByWordWrapping];

        CGRect renderingRectHeader = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, _pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSizeHeader.height);

        int ydistanceToLine = kBorderInset + kMarginInset + stringSizeHeader.height +kMarginInset;

        //Original Code that generated the issue
        //[textToDrawHeader drawInRect:renderingRectHeader withFont:fontHeader lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

        //Proposed change that resulted in an error
        [textToDrawHeader drawInRect:withAttributes:renderingRectHeader withFont:fontHeader lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
EN

回答 1

Stack Overflow用户

发布于 2017-04-03 15:24:07

-boundingRectWithSize:options:attributes:context:的返回类型为CGRect

例如:

代码语言:javascript
复制
CGRect textRect = [_lbl_title.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];

示例:

代码语言:javascript
复制
   - (float)expectedLabelHeight
{    
    CGSize boundingSize = CGSizeMake(CGRectGetWidth(_lbl_title.bounds), CGFLOAT_MAX);
    UIFont *font = [UIFont fontWithName:@"Roboto-Light" size:14];
    CGRect textRect = [_lbl_title.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];

    return (textRect.size.height+20.0);
}

根据上面的示例更改代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43161791

复制
相关文章

相似问题

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