繁体   English   中英

([NSAttributedString boundingRectWithSize:options:context:])方法无法在NSTextAttachment中获得正确的大小

[英]([NSAttributedString boundingRectWithSize:options:context:])method can not get the right size within NSTextAttachment

在我的代码中:

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"12123"];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"002"];
attachment.bounds = CGRectMake(0, 0, 20, 20);

[str insertAttributedString:[NSAttributedString attributedStringWithAttachment:attachment] atIndex:2];
CGRect rect = [str boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX)
                                options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                context:nil];

结构'rect'不正确,为什么?

我找到了答案,它对我来说是正常的。 我正在使用带有文本和附件的NSMutableAttributedString。

我创建了一个NSMutableAttributedString类,并添加了一个方法如下:

- (CGSize)getCoveredSizeForMaxSize:(CGSize)maxSize numberOfLines:(NSInteger)lines
{
    UILabel* dummyLabel = [UILabel new];
    [dummyLabel setFrame:CGRectMake(0, 0, maxSize.width, CGFLOAT_MAX)];
    dummyLabel.numberOfLines = lines;
    [dummyLabel setLineBreakMode:NSLineBreakByWordWrapping];
    dummyLabel.attributedText = self;
    [dummyLabel sizeToFit];

    return dummyLabel.frame.size;
}

希望这会对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM