Re: Vertically Centered Colon
Re: Vertically Centered Colon
- Subject: Re: Vertically Centered Colon
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Tue, 29 Nov 2016 16:33:26 +0700
> On 29 Nov 2016, at 03:33, Doug Hill <email@hidden> wrote:
>
> A couple of things to note:
>
[…]
> In any case it's a bad idea to select the exact font since you have to choose SF Display or Text which you don't want to do. It should be dynamically selected at runtime based on the font size. This behavior is supported by using System Font.
>
> Anyways, I would check the font at runtime to make sure it’s SF.
thinFont = <UICTFont:> font-family: “.SFUIDisplay-Ultralight"; font-weight: normal; font-style: normal; font-size: 70.00pt
> Otherwise, you need a font that supports the Vertically Centered Colon font feature. You can check this feature by doing the following:
> • Select your desired font on your Mac for the attributed string in the font panel
> • Click the Gear icon in the upper-left corner.
> • Select Typography…
> • Select 'Alternative Stylistic Sets'
> • Select ‘Vertically Centered Colon'
Well, this is kind of difficult: the Font Panel does not show fonts which have names which start with ‘.’.
So: no SF fonts in my Font Panel.
> Furthermore, I don’t see a way to set these 'Alternative Stylistic' attributes from UIAttributedText.
Can be done like this:
NSDictionary *featureCenteredColon = @{ UIFontFeatureTypeIdentifierKey: @(kStylisticAlternativesType),
UIFontFeatureSelectorIdentifierKey: @(kStylisticAltThreeOnSelector)
};
NSArray *featureArray = @[ featureCenteredColon ];
NSDictionary *featureAtribute = @{ UIFontDescriptorFeatureSettingsAttribute: featureArray };
UIFontDescriptor *thinDescriptor = thinFont.fontDescriptor;
UIFontDescriptor *newDescr = [ thinDescriptor fontDescriptorByAddingAttributes: featureAtribute ];
UIFont *betterThinFont = [ UIFont fontWithDescriptor: newDescr size: pointSize ];
> A little experimentation might find the right one.
A “little” is kind of misleading. There are about 40 features, with up to 20 alternatives each.
> OK, after doing some tests, UILabel indeed behaves like you mentioned with the SF font. If there aren't numbers on either side of the colon, no vertical centering. My guess would see if you can attempt to set the kStylisticAlternativesType attribute, as noted below, but as previously mentioned, will probably not work automatically due to the different fonts for the numerals around the colon.
>
> At this point, I’d say your working solution is probably as good as you're going to get.
After you kindly mentioned kStylisticAlternativesType I started to try this. The documentation is quite helpful in mentioning that some flag “Turns the nth set of alternates on or off” for n = 1 … 20.
And luckily kStylisticAltThreeOnSelector does indeed turn centred colons on.
So now I have two working solutions:
1. getting the special glyph for the centered colon (as suggested by Alistair) and:
2. using kStylisticAltThreeOnSelector (as suggested by you).
Thanks a lot. The hint of “kStylisticAlternativesType” was extremely helpful!
Kind regards,
Gerriet.
>
>
>> On Nov 28, 2016, at 11:12 AM, Gerriet M. Denkmann <email@hidden> wrote:
>>
>>
>>> On 28 Nov 2016, at 23:42, Alastair Houghton <email@hidden> wrote:
>>>
>>> On 28 Nov 2016, at 16:18, Gerriet M. Denkmann <email@hidden> wrote:
>>>>
>>>>
>>>>> On 28 Nov 2016, at 22:13, Eric E. Dolecki <email@hidden> wrote:
>>>>>
>>>>> You could probably use an attributed string and add an attribute for the last colon: NSBaselineOffsetAttributeName
>>>>
>>>> Yes; but this would be some rather desperate work-around.
>>>>
>>>> I was rather thinking of UIFontDescriptorFeatureSettingsAttribute with some Feature type from SFNTLayoutTypes.h (in CoreText).
>>>> I tried a few types, but no success so far.
>>>
>>> The problem you’ve got is that unless the font has a feature that specifically allows you to change *any* colon (as opposed to a colon between two numerals), you aren’t going to be able to do it by turning on an OpenType feature. Even if you can, there doesn’t appear to be a standard feature code for this, so you’d be reliant on Apple not changing it in the future.
>>
>> The WWDC 2015 talk seemed to suggest that there is a standard feature for this.
>> But there are about 40 feature types in SFNTLayoutTypes.h - no idea what to use.
>>
>>
>>> What you *could* do instead is get Core Text (or Cocoa Text) to lay out a string e.g. “12:00”, then grab the glyph for the centred colon directly from that string and use it explicitly, e.g. by attaching a kCTGlyphInfoAttributeName attribute to your string with the value set to an appropriately constructed CTGlyphInfoRef.
>>
>> done once:
>>
>> CGRect frame = { {0,0},{99,99}};
>> UITextView *dummyTextView = [ [UITextView alloc] initWithFrame: frame textContainer: nil ];
>> dummyTextView.text = @“23:21”;
>> dummyTextView.font = thinFont;
>> NSLayoutManager *layoutManager = dummyTextView.layoutManager;
>> [ layoutManager ensureGlyphsForCharacterRange: range ];
>> [ layoutManager ensureLayoutForCharacterRange: range ];
>> NSUInteger glyphIndex = [ layoutManager glyphIndexForCharacterAtIndex: 2 ];
>> centeredColonGlyph = [ layoutManager CGGlyphAtIndex: glyphIndex ];;
>>
>>
>> and then:
>>
>> CFMutableAttributedStringRef aStr = (__bridge CFMutableAttributedStringRef)attributedString;
>> CTFontRef fontRiff = (__bridge CTFontRef)thinFont;
>> CTGlyphInfoRef glyInfRef = CTGlyphInfoCreateWithGlyph( centeredColonGlyph, fontRiff, (CFStringRef)@":" );
>> CFRange range = { (CFIndex)colonIndex, 1 };
>> CFAttributedStringSetAttribute( aStr, range, kCTGlyphInfoAttributeName, glyInfRef );
>> CFRelease(glyInfRef);
>>
>> This seems to be working. Nut sure whether there is a more elegant way.
>>
>> Thanks a lot for your help!
>>
>>
>> Kind regards,
>>
>> Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden