NSAttributedString: Setting FontAttributes doesn't change font - ios

I am trying to change the font on an NSAttributedString.
I have a method which iterates over each character, and creates a new NSFontAttribute dictionary for that character. In the end, however, the string remains un-changed. To demonstrate, here is how I setup the string:
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:#"Avenir-Book" size:14.0f];
NSDictionary *fontAttributes = [fontDescriptor fontAttributes];
[fontAttributes setValue:[NSString stringWithFormat:#"%u",[fontDescriptor symbolicTraits]] forKey:UIFontSymbolicTrait];
[mutableAttributedString setAttributes:fontAttributes range:(NSRange){0,length}];
This produces the following NSFontAttribute dictionary for the entire string:
Font Attributes: {
NSCTFontSymbolicTrait = 2147483648;
NSFontNameAttribute = "Avenir-Book";
NSFontSizeAttribute = 14;
}
I go through and modify each character's FontAttribute to add bolding or italics, as follows:
for (int i = (int)range.location; i < (range.location + range.length); i++){
/* Extract Font Attributes */
NSDictionary *extractedAttributes = [[mutableAttributedString attributesAtIndex:i effectiveRange:NULL]mutableCopy];
/* Determine New Trait */
uint newTrait = ((uint)[[extractedAttributes valueForKey:UIFontSymbolicTrait]longLongValue] | [self symbolicTraitForMarkdownType:markdown]); // (markDown is a mask)
/* Set New Trait */
[extractedAttributes setValue:[NSString stringWithFormat:#"%u",newTrait] forKey:UIFontSymbolicTrait];
/* Create New Font Descriptor */
UIFontDescriptor *newDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:extractedAttributes];
newDescriptor = [newDescriptor fontDescriptorWithSymbolicTraits:newTrait];
/* Apply Font Descriptor */
[mutableAttributedString setAttributes:[newDescriptor fontAttributes] range:(NSRange){i,1}];
}
This produces many different FontAttributes:
Index 1: {
NSCTFontSymbolicTrait = 2147483650;
NSFontNameAttribute = "Avenir-Black";
NSFontSizeAttribute = 14;
}
Index 2: {
NSCTFontSymbolicTrait = 2147483651;
NSFontNameAttribute = "Avenir-BlackOblique";
NSFontSizeAttribute = 14;
}
However, the NSAttributedString itself remains completely un-changed. It is still the default Font. How can I get it to reflect the changes I am making to its attributes?

Here is the solution:
1: The documentation for UIFontDescriptor defines the key: UIFontDescriptorNameAttribute as an NSString instance, which it is.
2: The documentation for NSAttributedString defines the key: NSFontAttributeName as a UIFont instance.
So obtaining the fontAttributes dictionary from a UIFontDescriptor initialized with a the method: (UIFontDescriptor *)fontDescriptorWithName:(NSString *)fontName size:(CGFloat)size will only set the keys UIFontDescriptorNameAttribute and UIFontDescriptorSizeAttribute. If you wish to actually modify the font of an NSAttributedString however, you need to apply attributes with a UIFont instance saved to the key NSFontAttributeName.
This is where the confusion comes from. However, it should be noted that you can actually obtain UIFontDescriptorNameAttribute from the fontAttributes dictionary of a UIFontDescriptor instance with the key NSFontAttributeName. This may also be confusing.

Related

Attributed String

a text that has some part with yellow backgroundI need some padding to left and right of yellow colored text and need the label "3" to be center..I am using attributed text for it.can anyone help me on this.Below is the code i am using.Attaching screenshot as well.
string CardTileText = "3 Shared Documents";
NSAttributedString decoratedText = CardTileText.GetAttributedStringFromHtml("#ffe601","3");
public static NSAttributedString GetAttributedStringFromHtml(this string source, UIColor color, string identifier)
{
var atts = new UIStringAttributes();
UIFont newConnFont = UIFont.FromName("NotoSans-Bold", 16);
NSRange range = GetRangeFor(source, identifier);
NSNumber offset = 5;
NSMutableParagraphStyle para = new NSMutableParagraphStyle
{
Alignment = UITextAlignment.Left
};
NSMutableAttributedString attributedString = new NSMutableAttributedString(source, atts);
attributedString.AddAttribute(UIStringAttributeKey.BackgroundColor, color, range);
attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Black, range);
attributedString.AddAttribute(UIStringAttributeKey.Font, newConnFont, range);
attributedString.AddAttribute(UIStringAttributeKey.KerningAdjustment, offset, range);
attributedString.AddAttribute(UIStringAttributeKey.ParagraphStyle, para, range);
return attributedString;
}
static NSRange GetRangeFor(string source, string substring)
{
var range = new NSRange
{
Location = source.IndexOf(substring, StringComparison.Ordinal),
Length = substring.Length
};
return range;
}
I'm afraid the only solution is that set offset(which is used on KerningAdjustment) with a proper value.
Here is the comparison
offset = 0.5
offset = 10

Newline formatting is not supported in contentDescription property of CSSearchableItemAttributeSet

I am having a CSSearchableItem with CSSearchableItemAttributeSet,
CSSearchableItemAttributeSet* attributes = [[CSSearchableItemAttributeSet alloc]initWithItemContentType:(__bridge NSString *)kUTTypeContact];
attributes.keywords = #[keywords];
attributes.title = #"Contact Details";
attributes.displayName = #"Ram Gandhi";
attributes.emailAddresses = #[email];
attributes.phoneNumbers = #[phoneNumber];
attributes.contentDescription = [NSString stringWithFormat:#"%# \n %#", phoneNumber, email];
attributes.supportsPhoneCall = [NSNumber numberWithBool:YES];
attributes.supportsNavigation = [NSNumber numberWithBool:NO];
attributes.thumbnailData = [self getThumbnailData:contactID];
Everything is working fine but the newline in contentDescription is not working as expected though it is not displayed in the UI.
Also I am not sure about the purpose of using kUTTypeContact here as kUTTypeContent does the job. As far as I searched, there is no detailed documentation about how to use kUTTypeContact in Spotlight search.
Is there any other property that I can use inorder to show the email and phone number in separate lines?
it might be very late answer for you but it can help other. now , while using swift 4 we can add 2 lines "contentDescription" as below ,
var contentDescription = ""
var arrDescription = ["email","phone"]
for (index,strDesc) in arrDescription.enumerated(){
if index == 0{
contentDescription = strDesc
}else{
contentDescription += """
\(strDesc)
"""
}
}
I also have try to add more then two strings, but it display only upto two lines , then it will show ... at the end of second line.
Note: - check that "blank line", before "(strDesc)", it is not by mistake, it must be there to display newline.

How to check if a character is supported by a font

I'm working on an app with a text field. The text wrote in this field will be printed and I have an issue with some characters like emoji, chinese characters, etc... because the font do not provide these characters.
It's why I want to get all the character provided by a font (The font is downloaded so I can deal directly with the file or with an UIFont object).
I heard about CTFontGetGlyphsForCharacters but I'm not sure that this function do what I want and I can't get it work.
Here is my code :
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
NSString *characters = #"🐯"; // emoji character
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
NSLog(#"CTFontGetGlyphsForCharacters failed.");
Here CTFontGetGlyphsForCharacters return false. It's what I want because the character '🐯' is not provided by the font used.
The problem is when I replace NSString *characters = #"🐯" by NSString *characters = #"abc", CTFontGetGlyphsForCharacters return again false. Obviously, my font provide a glyph for all the ASCII characters.
I finally solve it :
- (BOOL)isCharacter:(unichar)character supportedByFont:(UIFont *)aFont
{
UniChar characters[] = { character };
CGGlyph glyphs[1] = { };
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)aFont.fontName, aFont.pointSize, NULL);
BOOL ret = CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, 1);
CFRelease(ctFont);
return ret;
}

NSString: strip out <b></b> and make an attributed string with color for that segment?

Say if I have a string:
This is a < b >simple < /b > string.
I need to get rid of the < b >, (sorry there is no space between b and angle bracket, for some reason the preview does not show it), also make the word 'simple' to be bold, my thought was:
replace the angle brackets and br with empty space
make the 'simple' segment to have attributes
The problem is once the tags are removed, I still need to know the word's location, do I first remember the location of 'simple', after removal, the location-4 should be the new location of 'simple'? Is there any better way? Or even transform html tag to attributes?
Thanks
edit:
Should be b instead of br
There is API available in iOS 7 that makes this very easy. It will convert an NSString of (possible) HTML text to an NSAttributedString.
NSDictionary *options = #{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType };
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithData:[myHTMLString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];
It will even preserve any in-line CSS applied, even background-color!
Note that if no font and font size is specified in the HTML text, the default will be Times New Roman 12. You could specify it in this fashion: <style>body { font-family:-apple-system; font-size:14px; }<style>. If you do not specify the font via CSS, you can still override the font, but you will need to manually handle bold, italics, etc otherwise that formatting will be lost if you set the font for the entire string. One approach is to enumerateAttribute: NSFontAttributeName on the mutable attributed string looking for 'bold' etc in the font name, and if it's found then replace that range with the desired font, such as the user's preferred font and size but the bold etc version of it, and continue replacing fonts with each range obtained from the enumeration.
The current answer is OK, and I have +1'd it. Yet it was only a clue, not a real solution.
If you are looking for a solution to the OP's question, take a look here.
You should focus on the following:
First implement these methods:
- (NSString *)styledHTMLwithHTML:(NSString *)HTML {
NSString *style = #"<meta charset=\"UTF-8\"><style> body { font-family: 'HelveticaNeue'; font-size: 20px; } b {font-family: 'MarkerFelt-Wide'; }</style>";
return [NSString stringWithFormat:#"%#%#", style, HTML];
}
- (NSAttributedString *)attributedStringWithHTML:(NSString *)HTML {
NSDictionary *options = #{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
return [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL error:NULL];
}
Later use them like that:
// This is a string that you might find in your model
NSString *html = #"This is <b>bold</b>";
// Apply some inline CSS
NSString *styledHtml = [self styledHTMLwithHTML:html];
// Generate an attributed string from the HTML
NSAttributedString *attributedText = [self attributedStringWithHTML:styledHtml];
// Set the attributedText property of the UILabel
label.attributedText = attributedText;
Use this method (Swift 5):
extension Swift {
// Color all strings between two tags and remove the tags
mutating func colorSubstringsBetweenTags(start: String, end: String, color: UIColor, font: UIFont? = nil) -> NSAttributedString {
var string = self
let attribute = NSMutableAttributedString(string: string)
while let openedEm = string.range(of: start, range: string.startIndex..<string.endIndex) {
let substringFrom = openedEm.upperBound
guard let closedEm = string.range(of: end, range: openedEm.upperBound..<string.endIndex) else { return attribute }
let substringTo = closedEm.lowerBound
let nsrange = NSRange(substringFrom..<substringTo, in: string)
if let font = font { attribute.addAttributes([.font: font], range: nsrange) }
attribute.addAttribute(.foregroundColor, value: color, range: nsrange)
attribute.mutableString.replaceCharacters(in: NSRange(closedEm, in: string), with: "")
attribute.mutableString.replaceCharacters(in: NSRange(openedEm, in: string), with: "")
string = attribute.mutableString as String
}
return attribute
}
}
Usage:
var text = "some text with <b>tags</b> or other <b>TAG</b>"
yourLabel.attributedText = text.colorSubstringsBetweenTags(start: "<b>", end: "</b>", color: .red)
Similar to Michael's answer just updated for latest swift version,
extension UILabel {
func setHTMLFromString(htmlText: String) {
// Apply some inline CSS
let style = "<meta charset=\"UTF-8\"><style> body { font-family: 'TheSansArabic-Plain'; font-size: 12px; } b {font-family: 'TheSansArabic-Bold'; font-size: 12px; } </style>"
let styledHtml = style + htmlText
// Generate an attributed string from the HTML
let attributedText = try? NSAttributedString(data: styledHtml.data(using: .utf8)!, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
// Set the attributedText property of the UILabel
self.attributedText = attributedText
}
}
Use In your code like this,
override func viewDidLoad() {
super.viewDidLoad()
label.setHTMLFromString(htmlText: "This is <b>BOLD text</b>")
}

core text - getting nsstring from ctrunref

I have the following code to get a run:
CFArrayRef runs = CTLineGetGlyphRuns(line);
for(int j = 0; j < CFArrayGetCount(runs); j++)
//for(id runObj in (__bridge NSArray*)CTLineGetGlyphRuns(line))
{
CTRunRef run = CFArrayGetValueAtIndex(runs, j);
}
What I need to do is to get the actual string of characters that contains this run. If I get the glyphs then these are not actual characters, is there any way to for example get an nsstring containing the actual text given a ctrun?
Thanks
CTRunRef run = (CTRunRef)runObj;
CFRange runRange = CTRunGetStringRange(run);
[[_attributedString attributedSubstringFromRange:NSMakeRange(runRange.location, runRange.length)] string]
No there is no way to get back the attributed string that was used in creation of a CTRun. Your program already ought to know this information, since it created the CTFrame, CTLine or CTRun from an attributed string.

Resources