首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么boundingRectWithSize在使用UIFont preferredFontForTextStyle时出错?

为什么boundingRectWithSize在使用UIFont preferredFontForTextStyle时出错?
EN

Stack Overflow用户
提问于 2015-06-18 03:55:31
回答 1查看 631关注 0票数 0

我发现对boundingRectWithSize的调用是非常不正确的,在使用NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]调用时,缺少了整个额外的行。然而,使用字体[UIFont fontWithName:@"Helvetica Neue" size:17.f],这是很好的。

下面是显示bug的测试代码:

代码语言:javascript
运行
复制
- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *measureText = @"I'm the king of Portmanteaus ... My students slow clap";

    CGSize maxSize = CGSizeMake(226.f, CGFLOAT_MAX);

    UIFont *targetFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

    CGRect stringRect = [measureText boundingRectWithSize:maxSize
                                                         options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                                      attributes:@{ NSFontAttributeName : targetFont }
                                                         context:nil];        

    UILabel *drawLabel = [[UILabel alloc] initWithFrame:stringRect];
    drawLabel.font = targetFont;
    [self.view addSubview:drawLabel];
    drawLabel.textColor = [UIColor blackColor];
    drawLabel.text = measureText;
    drawLabel.numberOfLines = 0;
}

以及产出:

但是,如果我做了targetFont = [UIFont fontWithName:@"Helvetica Neue" size:17.f];

它适当地渲染:

在第一种情况下,大小为{226.20200000000008,42.281000000000006},但在正确的第二种情况下,大小为{228.64999999999998,60.366999999999997}。因此,这不是一个四舍五入的问题;这是缺少一整条新行。

[UIFont preferredFontForTextStyle:UIFontTextStyleBody]作为参数传递给boundingRectWithSize是错误的吗

编辑

根据以下答复中的评论,我相信iOS如何处理与后续单词相关的三个句点存在缺陷。我添加了以下代码:

代码语言:javascript
运行
复制
measureText = [measureText stringByReplacingOccurrencesOfString:@" ..." withString:@"…"];

而且工作正常。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-18 04:36:55

标签在文本的外部添加了一点空白,而您不允许这样做。

如果您使用的是与字符串rect大小完全相同的自定义UIView,您将看到文本非常适合:

这是我用的密码。在视图控制器中:

代码语言:javascript
运行
复制
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *measureText = @"I'm the king of Portmanteaus ... My students slow clap";
    CGSize maxSize = CGSizeMake(226.f, CGFLOAT_MAX);
    UIFont *targetFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    CGRect stringRect = [measureText boundingRectWithSize:maxSize
                                                  options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                               attributes:@{ NSFontAttributeName : targetFont }
                                                  context:nil];
    stringRect.origin = CGPointMake(100,100);
    StringDrawer* sd = [[StringDrawer alloc] initWithFrame:stringRect];
    [self.view addSubview:sd];
    [sd draw:[[NSAttributedString alloc] initWithString:measureText attributes:@{ NSFontAttributeName : targetFont }]];
}

下面是字符串抽屉(一个UIView子类):

代码语言:javascript
运行
复制
@interface StringDrawer()
@property (nonatomic, copy) NSAttributedString* text;
@end

@implementation StringDrawer

- (instancetype) initWithFrame: (CGRect) frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor yellowColor];
    return self;
}

- (void) draw: (NSAttributedString*) text {
    self.text = text;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [self.text drawInRect:rect];
}

@end

另外,如果您的目的是通过调整文本的高度来制作包含文本的标签,为什么不让Auto来完成它的任务呢?下一个屏幕截图是一个UILabel,其宽度约束为226,没有高度约束。我给它分配了你的字体和文字,代码。正如您所看到的,它调整了自己的大小,以适应所有文本:

这是通过这一守则实现的,不需要更多:

代码语言:javascript
运行
复制
NSString *measureText = @"I'm the king of Portmanteaus ... My students slow clap";    
UIFont *targetFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.lab.font = targetFont;
self.lab.text = measureText;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30906188

复制
相关文章

相似问题

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