我的应用程序也是这样设置的。
自定义UIView在ScrollView中。
自定义UIView将在此代码的基础上生成文本。
CoreDavening.m绘图函数
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];在我的ViewController.m中,我创建的视图如下。
CoreDavening *davenView = [[CoreDavening alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, [coreDavening heightForStringDrawing])];
davenView.backgroundColor = [UIColor whiteColor];
davenView.layer.borderColor = [[UIColor whiteColor]CGColor];
scrollView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:davenView];
self.scrollView.contentSize = davenView.frame.size;我试图找到一种方法,1)改变字体大小(只是大小) 2)有适当的scrollView和CustomView调整大小。
这是如何做到的呢?
编辑1:我像这样在我的viewController中创建自定义视图。
CoreDavening *coreDavening = [[CoreDavening alloc]init];
davenView = [[CoreDavening alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [coreDavening heightForStringDrawing ])];并像这样更新/刷新自定义视图。
当我设置的滑块改变值时,就会触发它。
- (IBAction)sliderChanged:(id)sender {
NSUserDefaults *prefs= [NSUserDefaults standardUserDefaults];
[prefs setInteger:(long)self.Slider.value forKey:@"fontSize"];
[prefs synchronize];
[davenView setNeedsLayout];
}我的自定义视图控制器的字体设置类似。
这是在抽签函数中
attStorage = [[NSTextStorage alloc]initWithAttributedString:string];
[attStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:(long)[prefs integerForKey:@"fontSize"]] range:NSMakeRange(0, attStorage.length)];
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];发布于 2015-06-07 04:55:24
似乎您已经将textStorage与layoutManager相关联了。NSTextStorage是NSMutableAttributedString的子类。所以,这是负责保存布局和样式信息的类。您是如何创建textStorage的?此外,样式化,NSTextStorage也有方法允许编辑内容和具有动态样式。
如果要更改字体,可以简单地将属性设置为textStorage。
[_textStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, _textStorage.length)];NSTextManager类有非常有用的方法来查找边界和符号信息。所以,您可以使用下面这样的方法来找到文本的界限,
- (CGRect)boundingRectWithSize:(CGSize)size
{
self.textContainer.size = size;
NSRange glyphRange = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
CGRect boundingRect = [self.layoutManager boundingRectForGlyphRange:glyphRange
inTextContainer:self.textContainer];
boundingRect.origin.x = 0;
boundingRect.origin.y = 0;
return boundingRect;
}https://stackoverflow.com/questions/30690138
复制相似问题