Change Font Family for Specific Characters in my NSString - ios

My project demands that I use a custom Font, but this font have strange images in some characters like ( ) , . / etc...
The design agency said to replace the font in these characters to a more common font like Gill Sans.
So in a NSString = #"(My Favorite's. Love this!)";
I need to have the main custom font for the text and the Gill Sans font for the ( ' . ! and )
Is there a code where I can just pass the string and it returns some NSString with NSAttributedString with the font changes?
Thanks,

here is a sample code:
NSString *displayText = #"(My Favorite's. Love this!)";
NSMutableAttributedString *attributedString = [NSMutableAttributedString attributedStringWithString:displayText];
[attributedString setFont:[UIFont systemFontOfSize:15] range:[displayText rangeOfString:#"."]];
The last line will change the font of the point (.) to system font of size 15. You can search another characters in the string using the same function, and replace another properties of the string with setter methods of the form setX:range: of the NSAttributedString class.
Hope it helps!

use NSAttributedString to set multible font and size for string in ios NSAttributedString Class Reference,example

Related

Subscript and superscript Strings adding extra space between lines

Adding subscript and superscript to attributed string, making gaps between the lines get bigger than usual. I want all the lines to be equally spaced.
I have attached a screenshot for reference, which can show line gap difference between the first two lines and next two lines because of the subscripts added.
Can u help one help me out in this? Thanks in advance.
if ([start isEqualToString:#"<subscript>"]) {
[attributedString replaceCharactersInRange:replacingRange withString:tempString];
[attributedString addAttribute:(NSString *)kCTSuperscriptAttributeName value:#"-1" range:NSMakeRange(startRange.location, tempString.length)];
}
if ([start isEqualToString:#"<superscript>"]) {
[attributedString replaceCharactersInRange:replacingRange withString:tempString];
NSInteger textheight = 1;
CFNumberRef subscriptHeight = CFNumberCreate(NULL, kCFNumberNSIntegerType, &textheight);
[attributedString addAttribute:(NSString *)NSBaselineOffsetAttributeName value:(__bridge id _Nonnull)(subscriptHeight) range:NSMakeRange(startRange.location, tempString.length)];
}
The RTF tag for this is "\noextrasprl". I don't know if UITextView supports it but you could try assigning this as an attribute for the whole text (don't know if the attributes key will need the backslash or not)é

iOS - Is it possible to make UIFont SystemFont Italic and Thin (without using fontWithName:)?

My app uses only system fonts, and I am creating them with function -
+ (UIFont * _Nonnull)systemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight
How Can I make System font Italic with weight UIFontWeightThin?
I cannot use the call for specific family font (fontWithName:) since I want it to be system fonts only.
Thanks :)
You should create Font Descriptor at first that contain the type of your font if it Italic or Bold or Thin, etc..
UIFontDescriptor* desc = [UIFontDescriptor fontDescriptorWithFontAttributes:
#{
UIFontDescriptorFaceAttribute: #"Thin"
}
];
after that create a font object that hold the descriptor information
UIFont *font = [UIFont fontWithDescriptor:desc size:17];
So, set you font object to your label.
Now you got a font object using system font but you can change the the type and size of it without using fontWithName.
What about something like this:
[youLabel setFont:[UIFont fontWithName:[youLabel.font fontName] size:UIFontWeightThin]];

iOS 9 monospace text

I have a date countdown in my app in which I'd like to make use of San Francisco's monospaced number feature. I currently retrieve the font using this code:
UIFontDescriptor *countdownFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
NSArray *additionalFontSettings = #[#{UIFontFeatureTypeIdentifierKey: #(kNumberSpacingType), UIFontFeatureSelectorIdentifierKey: #(kMonospacedNumbersSelector)},
#{UIFontFeatureTypeIdentifierKey: #(kTextSpacingType), UIFontFeatureSelectorIdentifierKey: #(kMonospacedTextSelector)}];
countdownFontDescriptor = [countdownFontDescriptor fontDescriptorByAddingAttributes: #{UIFontDescriptorFeatureSettingsAttribute: additionalFontSettings}];
UIFont *desiredFont = [UIFont fontWithDescriptor:countdownFontDescriptor size:14];
I get monospaced numbers as they should appear, but I have semicolons in my string which separate the numbers. Occasionally I see this character move. How can I get the width of the semicolon to remain constant also? (This is why I tried adding kTextSpacingType)
Not sure but did you try looking in (as of Xcode 9.1 beta)
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreText.framework/Headers/SFNTLayoutTypes.h.
I can't see a way to do it there, but maybe someone else can.

Swift - Changing font size inside label

I have a label where there is text which should be bold and with another font size. Is there any possibility to do it like the line break ("Hello \n World!") with a command or do I have to make another label for this?
Thanks!
Look at the API for NSAttributedString -- it allows you to create a string that specifies portions of the string that should be styled with specific text styles and/or fonts. The resulting object can be used instead of a plain string with UILabel (and other UI elements) by setting the label's attributedText property instead of the usual text property.
To make just the word "bold" appear in 18 point bold, try something like the following:
var label = UILabel()
let bigBoldFont = UIFont.boldSystemFontOfSize(18.0)
var attrString = NSMutableAttributedString(string: "This text is bold.")
attrString.addAttribute(kCTFontAttributeName, value: bigBoldFont, range: NSMakeRange(13, 4))
label.attributedText = attrString
The range specified determines the portion of the string to which the named attributed (in this case, the font) should be applied. And note that the parameters to NSMakeRange are the starting character position and the length of the range.

get font-weight of an uitextview

I want to resize the font-size in some UITextViews. That works fine with an outlet collection and this code:
for (UITextView *view in self.viewsToResize) {
if ([view respondsToSelector:#selector(setFont:)]) {
[view setFont:[UIFont systemFontOfSize:view.font.pointSize + 5]];
}
}
But my problem is, that not every textView uses the systemFont in normal weight, some of them are in bold weight. Is it possible to get the font-weight? With a po view.font in the debug area I can see everything I need:
$11 = 0x0c596ea0 <UICFFont: 0xc596ea0> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12px
But how can I access the font-weight?
Using a second outlet collection for the bold views could solve my problem. But I'm wondering that I found nothing to get only the font-weight.
I have figured out how to get the font weights, you have to spelunk down to Core Text:
let ctFont = font as CTFont
let traits = CTFontCopyTraits(ctFont)
let dict = traits as Dictionary
if let weightNumber = dict[kCTFontWeightTrait] as? NSNumber {
print(weightNumber.doubleValue)
}
Enjoy!
UIFont does not have a bold/italic/... property, so you will have to rely on the font name only.
This will be a problem if you don't know which fonts will be used.
In the case you know that you will use eg. only Helvetica you can try this:
UIFont *font = textview.font;
if([font.fontName isEqualToString:#"Helvetica-Bold"])
NSLog(#"It's Bold!");
Alternatively you can search font.fontName for the word "bold"/"medium"/"light" etc., but that's not a guarantee you will get something from every available font:
if ([font.fontName rangeOfString:#"bold" options:NSCaseInsensitiveSearch].location == NSNotFound) {
NSLog(#"font is not bold");
} else {
NSLog(#"font is bold!");
}
// if font.fontName contains "medium"....
// if font.fontName contains "italic"....
Check http://iosfonts.com/ for the available font names.
But my problem is, that not every textView uses the systemFont in
normal weight, some of them are in bold weight.
If you want to use Bold System Font then you can simply use
[UIFont boldSystemFontOfSize:15.0];
However, I am still thinking of that special case in which you need to use font-weight.
Update :
There is nothing in the UIFont Class using which you can get font-weight directly. You can take a look at UIFont Class Reference.
Only thing that you can do is to get the font-name and try to find out the "bold" sub-string in the font name. If any match found that means font-weight of that specific font is "bold".
But, still this is not the most efficient method.
You can get a UIFontDescriptor for a font using the fontDescriptor method. Then you get the fontAttributes dictionary of the UIFontDescriptor, get the UIFontDescriptorTraitsAttribute from the dictionary which returns another dictionary, then read the UIFontWeightTrait from that dictionary. Not tested.
Now it's tested: Doesn't work. fontAttributes always returns a dictionary with two keys for font name and font size, and that's it. I suppose "this doesn't work" is also an answer when something should work according to the documentation...
You can try symbolicTraits, but that's not useful either: It returns "bold" only if the whole font family is bold.

Resources