gpt4 book ai didi

ios - 带有许多标签的 ScrollView

转载 作者:可可西里 更新时间:2023-11-01 02:16:13 25 4
gpt4 key购买 nike

我创建了一个 ScrollView 。

我正在使用 Storyboard,但是当我将这几个标签添加到我的 ViewController 并为它们设置正确的约束时,它在 iPhone 5s 上看起来不错,但在 iPhone 6s 上看起来很奇怪,因为下面的空间太多了最后一个标签。

所以当我在 ViewController 中设置我的 View 高度时,我应该如何去除更大设备上的那些空间?

我的约束可能有问题。

我在 iPhone 5s 上的看法:

enter image description here

我在 iPhone 6s 上的看法:

enter image description here

最佳答案

您可以使用此函数计算文本渲染大小:

//ARC code
//font: render font
//renderMaxWidth: max width of a line
//lineBreakMode: line break mode
- (CGSize) calculateLabelRenderSizeWith:(UIFont *) font renderMaxWidth:(CGFloat) width lineBreakMode:(NSLineBreakMode) mode
{
CGSize availableSize = CGSizeZero;
availableSize.width = width;
availableSize.height = MAXFLOAT;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = mode;

NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
NSDictionary *attributes = @{
NSFontAttributeName: font,
NSParagraphStyleAttributeName: style
};
CGSize textNodeSize = [self boundingRectWithSize:availableSize
options:options
attributes:attributes
context:nil].size;
return textNodeSize;
}

swift :

func __calculateLabelRenderSizeWith(font: UIFont, renderMaxWidth: CGFloat, lineBreakMode:NSLineBreakMode) -> CGSize
{
var availableSize = CGSizeZero
availableSize.width = renderMaxWidth
availableSize.height = CGFloat.max

let style = NSMutableParagraphStyle()
style.lineBreakMode = lineBreakMode

let attributes = [
NSFontAttributeName: font,
NSParagraphStyleAttributeName: style
]
let textNodeSize = boundingRectWithSize(availableSize, options: .UsesLineFragmentOrigin, attributes: attributes, context: nil).size
return textNodeSize;
}

关于ios - 带有许多标签的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378588/

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