【问题标题】:NSAttributedString relative font-size custom attributeNSAttributedString 相对字体大小自定义属性
【发布时间】:2013-11-12 18:17:00
【问题描述】:

在 CSS 中,您有 font-size 属性,可用于指定相对于文本其余部分的字体大小。

在 iOS 中实现自定义 relative font-size 属性需要什么(只需要与 iOS 7 兼容)?是否有任何第三方解决方案已经实现了这一点?

(我希望该解决方案适用于支持属性文本的所有内容:UILabel、UITextView、... UIButton 中的 titleLabel 等)

【问题讨论】:

    标签: ios nsattributedstring textkit


    【解决方案1】:

    假设您想为自己的自定义标题定义与用户首选正文字体大小(即动态类型)相关的属性。

    UIFontDescriptor *bodyFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
    
    NSNumber *bodyFontSize = bodyFont.fontAttributes[UIFontDescriptorSizeAttribute];
    
    float bodyFontSizeValue = [bodyFontSize floatValue];
    
    UIFont *headerOneFont = [UIFont fontWithDescriptor:bodyFontDescriptor size:bodyFontSizeValue * 3.0f];
    
    NSDictionary *headerOneAttributes = @{NSFontAttributeName : headerOneFont};
    
    UIFont *headerTwoFont = [UIFont fontWithDescriptor:bodyFontDescriptor size:bodyFontSizeValue * 2.0f];
    
    NSDictionary *headerTwoAttributes = @{NSFontAttributeName : headerTwoFont};
    

    此代码基于 raywenderlich.com 的“iOS 7 by Tutorials”第 4 章和第 5 章中的代码。

    属性字典可用于初始化属性字符串对象,而这些对象又可以显示在UILabels、UITextViews和UITextFields中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 2014-09-08
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多