我试图让用户自己调整字体大小来阅读应用内容。因此我放了一个滑块,如果用户向右滑动,字体将增长,左边意味着字体大小会更小。它工作正常,但问题是有时scrollView内容大小没有获得新的内容大小。 这是我的方法:
- (IBAction)enlargeLabelWithSlider:(id)sender{
UISlider *slider = (UISlider *)sender;
int font = 0;
int currentSliderValue = (int)slider.value;
if (currentSliderValue<=lastSilderValue) /* Every time the method call, at the end lastSilderValue = currentSliderValue*/
{
// Need to increase
font =lastSilderValue - currentSliderValue;
[self.gameTitleLabel setFont:[UIFont fontWithName:self.gameTitleLabel.font.fontName size:self.gameTitleLabel.font.pointSize+font]];
[self.gameScoreLabel setFont:[UIFont fontWithName:self.gameScoreLabel.font.fontName size:self.gameScoreLabel.font.pointSize+font]];
self.gameStateLabel.font = [UIFont systemFontOfSize:self.gameStateLabel.font.pointSize+font];
}
else
{
// Need to decrease
font =currentSliderValue - lastSilderValue;
[self.gameTitleLabel setFont:[UIFont fontWithName:self.gameTitleLabel.font.fontName size:self.gameTitleLabel.font.pointSize-font]];
[self.gameScoreLabel setFont:[UIFont fontWithName:self.gameScoreLabel.font.fontName size:self.gameScoreLabel.font.pointSize-font]];
self.gameStateLabel.font = [UIFont systemFontOfSize:self.gameStateLabel.font.pointSize-font];
}
lastSilderValue = currentSliderValue;
CGFloat height = self.gameStateLabel.frame.origin.y+self.gameStateLabel.frame.size.height+20;
CGSize size = CGSizeMake(self.ScrollView.frame.size.width, height);
self.ScrollView.contentSize = CGSizeMake(size.width, size.height); // i used self.gameStateLabel.frame.origin.y because it's the last label on the view.
}
有人能告诉我为什么会这样吗?感谢。
答案 0 :(得分:0)
我看不到UILabel
框架的任何变化。
也许您必须使此框架适应新的字体大小,然后尝试修改scrollview contentSize属性。
这将有所帮助:Adjust UILabel height depending on the text