gpt4 book ai didi

ios - 如何添加 NSString 以在定义大小的矩形中查看

转载 作者:行者123 更新时间:2023-11-29 03:02:08 25 4
gpt4 key购买 nike

我需要在我的 View 中添加一句话。但是这个句子相当大。所以我想把它放在一个定义的 CGRect 中,有几行,并改变字体大小,所有的句子都将在这个 CGRect 中可见。字体大小应尽可能大。

这里是我使用的代码:

NSAttributedString *sentence = [[NSAttributedString alloc] initWithString:@"The sentence with some words" attributes:@{NSFontAttributeName: wordsFont,NSBackgroundColorAttributeName:[UIColor yellowColor]}];


CGRect sentenceBounds;
sentenceBounds.size = [sentence size];
CGSize neededSize = CGSizeMake(MAX_WIDTH, MAX_HAIGHT);
sentenceBounds.size = neededSize;


sentenceBounds.origin = CGPointMake(0, 0);

[sentence drawInRect:sentenceBounds];

最佳答案

您正在寻找的所有功能都包含在 UILabel 中。您有什么理由不能只使用它吗?

UILabel *label = [UILabel new];
label.adjustsFontSizeToFitWidth = YES;
label.numberOfLines = 0;
label.attributedText = sentence;
label.frame = CGRectZero; //set desired frame here;
[self addSubview:label];

关于ios - 如何添加 NSString 以在定义大小的矩形中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com