【问题标题】:How to increase the font size of a NSAttributedsString in iOS如何在 iOS 中增加 NSAttributedstring 的字体大小
【发布时间】:2014-03-17 09:28:08
【问题描述】:

我正在尝试动态更改 NSAttributedString 的字体大小。问题是字符串包含不同的字体大小和属性。因此,当我更改字体大小时,所有内容大小都会更改为 tat 值。没有相应改变。 ... .

【问题讨论】:

    标签: ios7 xcode5 nsattributedstring uifont


    【解决方案1】:

    如果我对您的理解正确,这种方法应该对您有所帮助:您可以为您的 AttributedString 枚举所有 NSFontAttributeName 属性并将字体大小增加例如 1。这将为您提供以下结果:

    如果这是你想要的,这里就是实现这个的代码

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0., 0., 320., 320.)];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.backgroundColor = [UIColor whiteColor];
        self.label.numberOfLines = 0.;
        NSString *text = @"Small medium large";
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]     initWithString:text];
        [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, 6)];
        [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(6, 7)];
        [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(13, 5)];
        self.label.attributedText = attributedText;
        [self.view addSubview:self.label];
    
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(bumpFontSize) userInfo:nil repeats:YES];
    }
    
    - (void)bumpFontSize
    {
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:self.label.attributedText];
        [self.label.attributedText enumerateAttributesInRange:NSMakeRange(0., self.label.text.length) options:NSAttributedStringEnumerationReverse usingBlock:
         ^(NSDictionary *attributes, NSRange range, BOOL *stop)
        {
            NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
            UIFont *font = mutableAttributes[NSFontAttributeName];
            UIFontDescriptor *fontProperties = font.fontDescriptor;
            NSNumber *sizeNumber = fontProperties.fontAttributes[UIFontDescriptorSizeAttribute];
            [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:    [sizeNumber floatValue] + 1.] range:range];
         }];
        self.label.attributedText = attributedText;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2014-11-09
      • 2019-02-19
      • 2020-12-17
      • 2013-04-03
      • 1970-01-01
      • 2014-12-15
      • 2010-10-10
      相关资源
      最近更新 更多