标签: uifontdescriptor

从UIFontDescriptor创建UIFont时忽略UIFontWeightTrait和UIFontDescriptorFamilyAttribute

鉴于以下代码和运行iOS 7.1或更高版本的设备:

 NSDictionary *fontTraitsDictionary = @{UIFontWeightTrait : @(-1.0)};
 NSDictionary *attributesDictionary = @{
                                       UIFontDescriptorFamilyAttribute : @"Helvetica Neue", 
                                       UIFontDescriptorTraitsAttribute : fontTraitsDictionary
                                       };
 UIFontDescriptor *ultraLightDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:attributesDictionary];
 UIFont *shouldBeAnUltraLightFont = [UIFont fontWithDescriptor:ultraLightDescriptor size:24];

 NSLog(@"%@", shouldBeAnUltraLightFont);
Run Code Online (Sandbox Code Playgroud)

我希望它的值是shouldBeAnUltraLightFontHelveticaNeue-UltraLight的一个实例,但它是:

<UICTFont: 0x908d160> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 24.00pt
Run Code Online (Sandbox Code Playgroud)

据我所知,我正在关注Apple文档.为什么完全忽略字体系列和字体粗细信息?

我试过的事情

无论这些更改如何,返回的字体始终是正常重量的Helvetica的香草实例.

uifont ios7 uifontdescriptor uifontweighttrait

21
推荐指数
2
解决办法
5321
查看次数

iOS Swift为字体描述符添加自定义权重

这似乎应该是基本上容易的,但无论出于何种原因,它都无法正常工作.我在SO上看过类似的帖子,看起来Font Traits字典可能存在问题?这是一个问题还是我完全错过了什么?这是我第一次搞乱这样的字体,所以我认为可能是后者.

我不想使用常量或提供带有"-bold"变体的字体.我想对字体重量进行细粒度控制.

    let traits = [UIFontWeightTrait : 1.0]
    imgFontDescriptor = UIFontDescriptor(fontAttributes: [UIFontDescriptorNameAttribute: "Helvetica"])
    imgFontDescriptor = imgFontDescriptor.fontDescriptorByAddingAttributes([UIFontDescriptorTraitsAttribute:traits])
    imgTextFont = UIFont(descriptor: imgFontDescriptor!, size: 24.0)
Run Code Online (Sandbox Code Playgroud)

研究:

字体描述符在iOS 8中返回nil

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIFontDescriptor_Class/#//apple_ref/doc/constant_group/Font_Traits_Dictionary_Keys

根据iOS文档使用数值应该工作(我正在运行iOS 9.2):

UIFontWeightTrait作为NSNumber对象的标准化权重值.有效值范围为-1.0到1.0.值0.0对应于常规或中等字体粗细.您还可以使用字体粗细常量来指定特定的权重; 有关可以使用的常量列表,请参阅字体权重.适用于iOS 7.0及更高版本.

fonts ios swift uifontdescriptor

13
推荐指数
5
解决办法
9812
查看次数

在Swift 3/iOS10中使用Dynamic Type和自定义字体的更好方法

我试过两种方法:

方法1:

label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
label.adjustsFontForContentSizeCategory = true
Run Code Online (Sandbox Code Playgroud)

即使在"设置"中更改了首选文本大小,文本大小也会自动更改,即使在我返回应用程序之前,这也可以正常工作.它只适用于系统字体(旧金山).

方法2:

要使用自定义字体,我添加了一个扩展名UIFontDescriptor:

//from this answer http://stackoverflow.com/a/35467158/2907715
extension UIFontDescriptor {

    private struct SubStruct {
        static var preferredFontName: String = "Avenir-medium"
    }

    static let fontSizeTable : NSDictionary = [
        UIFontTextStyle.headline: [
            UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 23,
            UIContentSizeCategory.accessibilityExtraExtraLarge: 23,
            UIContentSizeCategory.accessibilityExtraLarge: 23,
            UIContentSizeCategory.accessibilityLarge: 23,
            UIContentSizeCategory.accessibilityMedium: 23,
            UIContentSizeCategory.extraExtraExtraLarge: 23,
            UIContentSizeCategory.extraExtraLarge: 21,
            UIContentSizeCategory.extraLarge: 19,
            UIContentSizeCategory.large: 17,
            UIContentSizeCategory.medium: 16,
            UIContentSizeCategory.small: 15,
            UIContentSizeCategory.extraSmall: 14
        ],
        UIFontTextStyle.subheadline: [
            UIContentSizeCategory.accessibilityExtraExtraExtraLarge: 21,
            UIContentSizeCategory.accessibilityExtraExtraLarge: 21,
            UIContentSizeCategory.accessibilityExtraLarge: 21,
            UIContentSizeCategory.accessibilityLarge: 21,
            UIContentSizeCategory.accessibilityMedium: 21, …
Run Code Online (Sandbox Code Playgroud)

fonts ios swift uifontdescriptor dynamic-type-feature

9
推荐指数
1
解决办法
6346
查看次数

在iOS上显示按比例分隔的数字(而不是等宽/表格)

我通过将它们存储在NSAttributedString中并使用"drawAtPoint:"进行渲染,在iOS中渲染数字(目标为7及以上).我正在使用Helvetica Neue.

我注意到像这样绘制的数字的数字不成比例 - 字形都具有相同的宽度.即使是瘦的"1"也占用与"0"相同的空间.

测试证实了这一点:

for(NSInteger i=0; i<10; ++i)
{
  NSString *iString = [NSString stringWithFormat: @"%d", i];
  const CGSize iSize = [iString sizeWithAttributes: [self attributes]];
  NSLog(@"Size of %d is %f", i, iSize.width);
}
Run Code Online (Sandbox Code Playgroud)

在其他地方:

-(NSDictionary *) attributes
{
  static NSDictionary * attributes;
  if(!attributes)
  {
    attributes = @{
                   NSFontAttributeName: [UIFont systemFontOfSize:11],
                   NSForegroundColorAttributeName: [UIColor whiteColor]
                   };
  }
  return attributes;
}
Run Code Online (Sandbox Code Playgroud)

这个结果字形都具有 6.358点的相同宽度.

是否有一些渲染选项我可以启用它来启用比例数字字形?是否有另一种字体(理想情况下类似于Helvetica Neue)支持比例数字字形(理想情况下,内置)?还要别的吗?

谢谢.

nsstring uifont ios uifontdescriptor

7
推荐指数
1
解决办法
2841
查看次数

字体描述符在iOS 8中返回nil

以下代码在iOS 7中工作正常,但在iOS 8中不返回粗体或斜体字体.对于Helvetica Neue,它可以使用,但对Arial字体不起作用.

UIFontDescriptor *descriptor1 = [UIFontDescriptor fontDescriptorWithFontAttributes:@{UIFontDescriptorFamilyAttribute: @"Arial"}];
UIFontDescriptor* boldFontDescriptor1 = [descriptor1 fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFont* font1 = [UIFont fontWithDescriptor:boldFontDescriptor1 size:16.0];
[self.lblArialB setFont:font1];
Run Code Online (Sandbox Code Playgroud)

在设备和模拟器上测试,仍然是相同的错误.

ios xcode6 ios8 uifontdescriptor

6
推荐指数
1
解决办法
2085
查看次数

如何找到UILabel的现有UIFontTextStyle?

我目前正在创建扩展,UILabel以方便观察动态类型。一旦UIContentSizeCategoryDidChangeNotification收到,我想我的选择通过使用设置标签的字体

self.font = UIFont.preferredFontForTextStyle(someUIFontTextStyle)
Run Code Online (Sandbox Code Playgroud)

其中,someUIFontTextStyle使用相同的UIFontTextStyle标签目前呈现。我曾希望可以通过类似这样的属性来访问这样的属性self.font.fontDescriptor.textStyle,但事实似乎有点令人费解。

有没有办法访问与UIFontTextStyle关联的属性UILabel

self.font.fontDescriptor().objectForKey(UIFontDescriptorTextStyleAttribute) as? String
Run Code Online (Sandbox Code Playgroud)

iphone uilabel ios swift uifontdescriptor

5
推荐指数
1
解决办法
1339
查看次数

获取内容大小类别的 UIFont

UIFont提供了+preferredFontForTextStyle:获得字体实例与适当大小的方法,根据用户选择的内容大小类别和给定UIFontTextStyle

我想做的是为给定的文本样式内容大小获取字体。类似的东西+fontForContentSizeCategory:andTextStyle:

不幸的是,我在UIFont或的标题中找不到任何类似的内容UIFontDescriptor

关于如何实现这一目标的任何想法?

谢谢

uifont ios uifontdescriptor

4
推荐指数
2
解决办法
1068
查看次数

当 CTFeatureTypeIdentifier 不可用时如何使用 UIFontDescriptor

我使用以下代码片段来获取字体功能:

let font = UIFont.systemFont(ofSize: 16)
let features: NSArray = CTFontCopyFeatures(font)!
print("properties = \(features)")
Run Code Online (Sandbox Code Playgroud)

所以如果我看到这个:

{
CTFeatureTypeExclusive = 1;
CTFeatureTypeIdentifier = 22;
CTFeatureTypeName = "Text Spacing";
CTFeatureTypeSelectors =         (
                {
        CTFeatureSelectorDefault = 1;
        CTFeatureSelectorIdentifier = 7;
        CTFeatureSelectorName = "No Change";
    },
                {
        CTFeatureSelectorIdentifier = 8;
        CTFeatureSelectorName = "No Kerning";
    }
);
Run Code Online (Sandbox Code Playgroud)

我可以将其翻译成 Swift:

 let fontDescriptorFeatureSettings = [
            [ UIFontDescriptor.FeatureKey.featureIdentifier : 22,
              UIFontDescriptor.FeatureKey.typeIdentifier : 8], ]
Run Code Online (Sandbox Code Playgroud)

这成功地为我禁用了字距调整。

在同一个片段中,我看到:

{
CTFeatureOpenTypeTag = cv05;
CTFeatureSampleText = I;
CTFeatureTooltipText = "\U00a9 2015-2022 Apple Inc. …
Run Code Online (Sandbox Code Playgroud)

xcode ios ctfont uifontdescriptor

3
推荐指数
1
解决办法
338
查看次数

如何为 UIFontMetrics 指定最小 UIContentSizeCategory?

我有一种基于动态类型创建自动缩放字体的方法,如下所示:

extension UIFont {
    public static func getAutoScalingFont(_ fontName: String, _ textStyle: UIFont.TextStyle) -> UIFont {
        // getFontSize pulls from a map of UIFont.TextStyle and UIFont.Weight to determine the appropriate point size
        let size = getFontSize(forTextStyle: textStyle)
        guard let font = UIFont(name: fontName.rawValue, size: size) else { 
            return UIFont.systemFont(ofSize: size)
        }
        let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
        let traitCollection = UITraitCollection(preferredContentSizeCategory: UIApplication.shared.preferredContentSizeCategory)
        let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle, compatibleWith: traitCollection)
        return fontMetrics.scaledFont(for: font, maximumPointSize: fontDescriptor.pointSize)
    }
}
Run Code Online (Sandbox Code Playgroud)

这看起来效果很好;当我更改手机设置中的文本大小滑块值时,字体会根据需要进行缩放。

我现在尝试添加最小UIContentSizeCategory 的逻辑。也就是说,如果用户将其文本大小值设置为小于我指定的最小大小类别,则字体应按比例缩放,就像他们选择了最小值一样。 …

uifont ios swift uifontdescriptor uitraitcollection

2
推荐指数
1
解决办法
1598
查看次数

设置 UIFont 粗细

在 iOS 8 中,为了获得 Helvetica Neue 的瘦变体,以下代码将起作用

UIFont.systemFontOfSize(50, weight: UIFontWeightThin)
Run Code Online (Sandbox Code Playgroud)

在 iOS 9 系统字体更改为 San Francisco,所以我不能再这样做了。我绝对必须在这个应用程序中使用 Helvetia 并且无法弄清楚如何设置字体粗细。这是我目前使用的代码。

    timeSelectedLabel.font = UIFont(name: "Helvetica Neue", size: CGFloat(50))
Run Code Online (Sandbox Code Playgroud)

我知道有一个接受 UIFontDescriptor 的构造函数。虽然不确定如何使用它。任何想法表示赞赏!

注意:在将其标记为重复之前,请了解仅有的 2 个帖子要么未答复,要么当前不起作用。

uifont ios uifontdescriptor

1
推荐指数
1
解决办法
6053
查看次数

项目转换为Swift 4后,'pod spec lint'失败

在将我的cocoa框架项目转换为Swift 4后,类UIFontDescriptorFamilyAttribute现在是UIFontDescriptor.AttributeName.family,所以我更改了我的代码:

// Swift 3    
UIFontDescriptor(fontAttributes: [UIFontDescriptorFamilyAttribute: fontFamiliy])
Run Code Online (Sandbox Code Playgroud)

// Swift 4
UIFontDescriptor(fontAttributes: [UIFontDescriptor.AttributeName.family: fontFamiliy])
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试 - pod spec lint - 我得到下一个错误:

- ERROR | [iOS] xcodebuild:  SerializableLabel.swift:108:68: error: type 'UIFontDescriptor' has no member 'AttributeName'
Run Code Online (Sandbox Code Playgroud)

cocoapods还不知道Swift 4吗?我是否必须在配置中更新其他内容?

我的配置:

.podspec

s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' }
Run Code Online (Sandbox Code Playgroud)

的CocoaPods

$ pod --version
1.3.1
Run Code Online (Sandbox Code Playgroud)

cocoapods swift uifontdescriptor swift4

1
推荐指数
1
解决办法
354
查看次数

iOS 14 中的可变字体

以下代码允许创建具有不同权重的字体。

func makeFont(weight: CGFloat, size: CGFloat) -> UIFont {
    var attributesDict = [String: Any]()
    attributesDict["Weight"] = weight
    /* Rubik-Light - is a variable font */
    let fontDescriptor = UIFontDescriptor(
        fontAttributes: [
            UIFontDescriptor.AttributeName.name : "Rubik-Light",
            kCTFontVariationAttribute as UIFontDescriptor.AttributeName : attributesDict
        ]
    )
    return UIFont(descriptor: fontDescriptor, size: size)
}
Run Code Online (Sandbox Code Playgroud)

它在 ios 13 及更低版本上运行良好,但在 iOS 14 上不起作用。有什么解决方案吗?

ios swift uifontdescriptor ios14 variable-fonts

0
推荐指数
1
解决办法
326
查看次数

将Object-C转换为SWIFT textKit

我的应用程序中使用了以下代码来更改textview中text和big和italic之间的状态.我一直试图在Swift中重写它,但我不断收到编译错误.以下是在线教程中提供给我的Objective-C版本.接下来是我的新swift版本,直到该方法崩溃.
我会推荐任何帮助我理解为什么编译器抱怨这么多的帮助.

-(void)addOrRemoveFontTraitWithName:(NSString *)traitName andValue:(uint32_t)traitValue{
NSRange selectedRange = [_field2 selectedRange];


NSDictionary *currentAttributesDict = [_field2.textStorage attributesAtIndex:selectedRange.location
                                                                effectiveRange:nil];


UIFont *currentFont = [currentAttributesDict objectForKey:NSFontAttributeName];
UIColor *currentColor = [currentAttributesDict objectForKey:NSForegroundColorAttributeName];
UIColor *currentBGColor = [currentAttributesDict objectForKey:NSBackgroundColorAttributeName];
UIFont *currentUnderlinedText = [currentAttributesDict objectForKey:NSUnderlineStyleAttributeName];
NSMutableParagraphStyle *currentparagraphStyle = [currentAttributesDict objectForKey:NSParagraphStyleAttributeName];    //receive and set new font name



UIFontDescriptor *fontDescriptor = [currentFont fontDescriptor];


NSString *fontNameAttribute = [[fontDescriptor fontAttributes] objectForKey:UIFontDescriptorNameAttribute];
UIFontDescriptor *changedFontDescriptor;

if ([fontNameAttribute rangeOfString:traitName].location == NSNotFound) {


    uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | traitValue;
    changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];

}
else{
    uint32_t …
Run Code Online (Sandbox Code Playgroud)

objective-c ios swift uifontdescriptor

-2
推荐指数
1
解决办法
388
查看次数