我有一个文本视图和按钮。出于某种原因,我试图在这两个元素之间设置适当的间隙时得到意想不到的结果。我认为text.view框架的计算可能不正确。这就是我设法设置text.view框架的方法:
//1 Setting properties for text, starting with dictionary containing font size and family
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"helvetica neue"
size:14]
forKey: NSFontAttributeName];
//Creating frame to hold textView
CGSize maximumLabelSize = CGSizeMake(self.myTextView.frame.size.width, FLT_MAX);
CGSize textViewSize = [self.descriptionStringShort boundingRectWithSize:maximumLabelSize
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
//Setting frame
CGRect frame = self.myTextView.frame;
frame.size.height = textViewSize.height;
NSLog(@"%f height", frame.size.height);
self.myTextView.frame = frame;
在那之后,当试图设置按钮帧时,它总是显示元素之间的差异(我的“粗略”解决方案是检查text.view的高度并设置“minimum”和“maximum”y取决于它,但是它不适用于真实的应用程序)。例如,按钮框可以在textView.frame上绘制,也可以在其下方绘制。我如何设置地图框架:
CGRect mapButtonFrame = self.mapButton.frame;
mapButtonFrame.origin.x = 30;
if (frame.size.height < 50){
mapButtonFrame.origin.y = (200+frame.size.height + 150);
} else if (frame.size.height >300){
mapButtonFrame.origin.y = (200+frame.size.height +50);
} else {
mapButtonFrame.origin.y = (200+frame.size.height +100);
}
self.mapButton.frame = mapButtonFrame;
不过,这种方法很糟糕,请看一下附带的截图:
在这里,正如您所看到的,2个元素之间总是存在差异。我怎么能修好它? 顺便说一句,关闭自动布局。 任何建议将不胜感激,谢谢1
答案 0 :(得分:1)
这应该可以正常工作:
[myTextView sizeToFit];//This will change the textView frame according to the text length
CGRect bFrame = self.mapButton.frame;
bFrame.origin.y = myTextView.frame.size.height + 20.0f;// The gap you want between the text and the button
self.mapButton.frame = bFrame;