我已经使用 NSLayoutManager (TextKit) 设置了一个 UIView,并将 UIView 放置在 UIScrollView 中。我无法将 UIView 的大小调整为文本的大小,以便可以滚动它。
目前,文本填满了屏幕,但 scrollView 并没有真正滚动到它的其余部分。
UIView 代码:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
long value = [prefs integerForKey:@"Daven"];
if((int)value == 0){
string =
[NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Shacharit" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:NULL
];
}else if((int)value == 1){
string =
[NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Mincha" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:NULL
];
}else if((int)value == 2){
string =
[NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Maariv" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:NULL
];
}else if((int)value == 3){
string =
[NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"ּBrachaAchrona" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:NULL
];
}else if((int)value == 4){
string =
[NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"BirkatHamazon" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:NULL
];
}else if((int)value == 5){
string =
[NSAttributedString.alloc
initWithFileURL:[ NSBundle.mainBundle URLForResource:@"TefillatHaderech" withExtension:@"rtf" ]
options:nil
documentAttributes:nil
error:NULL
];
}
attStorage = [[NSTextStorage alloc]initWithAttributedString:string];
textContainer = [[NSTextContainer alloc]
initWithSize:CGSizeMake(self.frame.size.width, FLT_MAX)];
layoutManager = [[NSLayoutManager alloc]init];
[layoutManager addTextContainer:textContainer];
[attStorage addLayoutManager:layoutManager];
NSRange glyphRange = [layoutManager
glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: rect.origin];
}
使用
(void) [layoutManagerr glyphRangeForTextContainer:textContainerr];
NSLog(@"WORK %f", [layoutManagerr
usedRectForTextContainer:textContainerr].size.height);
我得到一个数字(我认为是文本的正确高度)。
如何使用返回的数字来调整 UIView 的大小。
在我的 ViewController.m 我尝试使用
CoreDavening *coreDavening = [[CoreDavening alloc]init];
self.daveningView.frame = CGRectMake(self.daveningView.frame.origin.x, self.daveningView.frame.origin.y, self.daveningView.frame.size.width,[coreDavening heightForStringDrawing]);
self.daveningLabel.contentSize = CGSizeMake(self.view.frame.size.width,[coreDavening heightForStringDrawing]);
附言。UIView 作为 IBOutlet 附加到 UIViewController 并称为 daveningView,但 UIView 的自定义类名称是 CoreDavening。DaveningLabel 是 ScrollView(我知道它对于我计划修复的 scrollView 的措辞不好)
编辑 1:视图控制器代码:
没有限制。
self.daveningView.frame = CGRectMake(self.daveningView.frame.origin.x, self.daveningView.frame.origin.y, self.daveningView.frame.size.width, [coreDavening heightForStringDrawing]);
self.daveningLabel.contentSize = CGSizeMake(self.view.frame.size.width,[coreDavening heightForStringDrawing]);