首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NSTextContainer中更改字体

如何在NSTextContainer中更改字体
EN

Stack Overflow用户
提问于 2015-06-07 04:44:50
回答 1查看 1.1K关注 0票数 1

我的应用程序也是这样设置的。

自定义UIView在ScrollView中。

自定义UIView将在此代码的基础上生成文本。

CoreDavening.m绘图函数

代码语言:javascript
复制
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中,我创建的视图如下。

代码语言:javascript
复制
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中创建自定义视图。

代码语言:javascript
复制
CoreDavening *coreDavening = [[CoreDavening alloc]init];

     davenView = [[CoreDavening alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [coreDavening heightForStringDrawing ])];

并像这样更新/刷新自定义视图。

当我设置的滑块改变值时,就会触发它。

代码语言:javascript
复制
- (IBAction)sliderChanged:(id)sender {

    NSUserDefaults *prefs= [NSUserDefaults standardUserDefaults];
    [prefs setInteger:(long)self.Slider.value forKey:@"fontSize"];
    [prefs synchronize];

    [davenView setNeedsLayout];
}

我的自定义视图控制器的字体设置类似。

这是在抽签函数中

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

发布于 2015-06-07 04:55:24

似乎您已经将textStorage与layoutManager相关联了。NSTextStorage是NSMutableAttributedString的子类。所以,这是负责保存布局和样式信息的类。您是如何创建textStorage的?此外,样式化,NSTextStorage也有方法允许编辑内容和具有动态样式。

如果要更改字体,可以简单地将属性设置为textStorage。

代码语言:javascript
复制
[_textStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, _textStorage.length)];

NSTextManager类有非常有用的方法来查找边界和符号信息。所以,您可以使用下面这样的方法来找到文本的界限,

代码语言:javascript
复制
- (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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30690138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档