Trouble with formatting of OHAttributedLabel when parsing markup - ios

In a property list I have a string like this:
<string>A |picture| is worth *1000\* words.</string>
Obviously "picture" should be italic and "1000" should be bold. I'm trying to display the text using OHAttributedLabel:
NSString *theString = [pListData objectForKey:#"theString"];
NSMutableAttributedString* attrStr = [OHASBasicMarkupParser attributedStringByProcessingMarkupInString:theString];
self.myLabel.attributedText = attrStr; //self.myLabel is a UILabel
And here is what I get:
Why are the attributed words displayed in a smaller font, and how do I fix this?

I have deal with something like this before. So, this will help you. Also i have tested it for you and works fine :)
NSString *string = #"A |picture| is worth *1000* words.";
// Create an NSMutableAttributedString in order to be able to use the NSAttributedString+Attributes.h category
NSMutableAttributedString *attributedString = [NSMutableAttributedString attributedStringWithString:string];
// If using custom fonts, import all styles in the app and update the plist file
[attributedString setFont:[UIFont fontWithName:#"Helvetica" size:20.0]];
// Parse the markup and set the attributedText of the UILabel instance (no need to use the OHAttributedLabel subclass)
attributedString = [OHASBasicMarkupParser attributedStringByProcessingMarkupInAttributedString:attributedString];
label.attributedText = attributedString;
EDIT: The problem comes when using -setFont: after the calling -attributedStringByProcessingMarkupInAttributedString, since that would reset all the work done during parsing.

Related

How to increase font size of whole content in NSMutableAttributedString

I want to increase font size of text that comes from server and text is in html format.I used NSAttributedString for showing text.
NSRange rangeOfTitle = NSMakeRange(0,[catDescLabel length]);
NSMutableAttributedString *string1 =
[[NSMutableAttributedString alloc] initWithString:catDescLabel];
[string1 addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20.0]
range:rangeOfTitle];
I am attaching screenshot that how it look and i am also attaching screenshot how it look in Android.I want to increase font size of whole content. how can i do this ?
Could anyone give me any Idea or help. Much appreciated
Android Screenshot
iOS Screenshot
Thanks ,
addAttribute ... but it seems it is not available for NSAttributedString
But it is available for NSMutableAttributedString.
That's how it is with all Cocoa immutable/mutable class pairs. The immutable member of the pair is, uh, immutable — that means you can't change anything about it. If you want to change something about it, convert it to the mutable member of the pair.
NSMutableAttributedString *attributedString2 =
[[NSMutableAttributedString alloc] initWithAttributedString: attributedString1];
You would do something like this…
NSMutableAttributedString *string1 =
[[NSMutableAttributedString alloc] initWithString:#"Your string"];
[string1 addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20.0]
range:NSMakeRange(5, 6)];
Update :
[self.label setAttributedTitle:string1];
This will set the last two words in 20-point text; the rest of the string will use the default value (which I believe is 12 points). The thing that might be confusing about setting the text size is that you have to set the typeface and the size at the same time—each UIFont object encapsulates both of those properties.
And reference link for you
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/CreatingAttributedStrings.html

Update the UITextView content dynamically in a specific range

I need to update the words dynamically in UiTextView content while TTS is playing without refresh the page.
Problem: While tts playing for the each word delegate(willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance) is calling.
so at that time i am updating the text color using NSMutableAttributedString.
In this process the content in the textview is unstable because for each word page is refreshing.
so i need to update the text in textview without refreshing the page.
I wrote the code like this.
NSMutableAttributedString *attrStr = [[NSMutableAttributedString
alloc] initWithString:_theContent ];
[attrStr addAttribute:NSFontAttributeName value:[UIFont
systemFontOfSize:self.fontSize] range:[self wholeArticleRange]];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor
redColor] range:theRange];
textview.attributedText = attrStr;
--In android we used spannable class to do this way. Is there any class in IOS like this?
Kindly give us solution ASAP
Thanks in advance.
You can access the text view's textStorage property, of type NSTextStorage, which is a subclass of NSMutableString. From there you can use any NSMutableString operation, including setting attributes on the various ranges, without sending an entirely new string to the text view (which will make it flicker).
Do not forget to call beginEditing and endEditing to group edits together if you have to make multiple calls, in order to improve performance.
I wrote the code like this. I am new to the IOS so if i am wrong kindly correct me.
(NSMutableDictionary *)attDict {
if (!_attDict) {
_attDict = [NSMutableDictionary dictionary];
UIFont *font = [UIFont systemFontOfSize:self.fontSize];
// UIFont *font = [UIFont fontWithName:#"TimesNewRomanPSMT" size:self.fontSize];
[_attDict setObject:font forKey:NSFontAttributeName];
[_attDict setObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName];
}
return _attDict; }
[[displayingArticleView contentView].textStorage
setAttributes:self.attDict range:theRange] ;
Thanks
If you just want to replace any text in your UITextField with some other on the fly, You can use the string replacement method, Like
[textField.text stringByReplacingOccurrencesOfString:#"your string" withString:#"string to be replaced"];
This will Replace all occurrences of the target(string to be replaced) string with replacement.

UILabel appearance font and attributed string font

In my app I have a global custom font applied to all labels like so:
UIFont *font = [UIFont fontWithName:kMyFontName size:15.0];
[[UILabel appearance] setFont:font];
This works fine. However, in some cases I want to be able to specify a different font for a specific region of a UILabel string.
So I have something like this:
NSString *string = #"Foo Bar Baz";
UIFont *boldFont = [UIFont fontWithName:kMyBoldFontName size:15.0];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
[attrString setAttributes:#{ NSFontAttributeName: boldFont } range:NSMakeRange(0, 3)];
self.myLabel.attributedText = attrString;
However this doesn't seem to work. I expect the "Foo" to be bold, but the entire string just has the default font. It's as if the bold font is not applied at all and is being overwritten by the font set on the UILabel appearance proxy.
When I remove the UILabel appearance line then it works fine (I can see part of the string in bold). Basically I want to have my custom font applied to the label but a separate font applied to a different region of the string. Normally this works fine with attributed strings but for some reason setting the UILabel appearance font disables this functionality (or so it seems).
Expected results: "Foo Bar Baz"
Actual results: "Foo Bar Baz"
If I remove the [[UILabel appearance] setFont:] line then it works:
"Foo Bar Baz"
(but the custom font is not set on the rest of the string).
So my question is: Is there a way to specify a single font to use as the default app-wide but still be able to partially override that using attributed strings?
Also if someone can explain to me why this is not working I'd appreciate it.
Set font and textColor to nil just before setting attributed string.
You can't mix and match attributed text and plain text; that's why removing the setFont method works - because when you use it, it assumes a plaintext UILabel.
NSString *string = #"Foo Bar Baz";
UIFont *boldFont = [UIFont fontWithName:kMyBoldFontName size:15.0];
// Define your regular font
UIFont *regularFont = [UIFont fontWithName:kMyFontName size:15.0];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
// And before you set the bold range, set your attributed string (the whole range!) to the new attributed font name
[attrString setAttributes:#{ NSFontAttributeName: regularFont } range:NSMakeRange(0, string.length - 1)];
[attrString setAttributes:#{ NSFontAttributeName: boldFont } range:NSMakeRange(0, 3)];
self.myLabel.attributedText = attrString;
There are two important parts:
UIAppearance applies at the moment of adding UI element to window
labelInstance.font = ... resets all font attributes of currently set attributed string
So if you want to keep UIAppearance customisation you have to set your custom attributed string after your label get added to window.
Reference article about how does UIAppearance work: Peter Steinberger's cool article

how to set a UITextView's text to be bolded and not clickable

I have the following HTML in a UITextView and would like to render it into a UITextView
is my body for the note
food item - more item stuff;`
Let me add: it's currently showing as blue and underlined and not clickable. I would like to make it bolded and not clickable. I have read the docs regarding linkTextAttributes but, not having used this, it is a bit beyond me and I don't really see any easy way to manipulate this. How would I just render the above link bolded and black (not blue) and maintain the non-clickable nature?
UPDATE (solution using UITextView's linkTextAttributes)
self.testTextView.editable = NO;
self.testTextView.selectable = YES;
self.testTextView.userInteractionEnabled = NO; // workaround to disable link - CAUTION: it also disables scrolling of UITextView content
self.testTextView.dataDetectorTypes = UIDataDetectorTypeLink;
self.testTextView.linkTextAttributes = #{NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0f], // NOT WORKING !?
NSForegroundColorAttributeName : [UIColor redColor]};
...
self.testTextView.text = #"Lorem ipsum http://www.apple.com Lorem ipsum";
As you can see in comments, I wasn't able to set new font to linkTextAttributes, though the colour attribute was working as expected.
If you can get away with colour attribute or some other text attribute to style your URLs and you don't have to worry about disabled UITextView scrolling, then this may be your solution.
PREVIOUS (alternative solution)
If you're using Storyboard/xib then make sure you've deselected Detection -> Links for your UITextView. You can make your link bold by setting its container font to some bold typeface. If you want to support different text/font styles in one string object then you should really look for NSAttributedString or NSMutableAttributedString.
See: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSAttributedString_Class/Reference/Reference.html.
Example:
UIFont *linkFont = [UIFont fontWithName:#"SomeBoldTypeface" size:12];
NSString *link = #"food item - more item stuff";
NSMutableAttributedString *someString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"is my body for the note %#; let me ad", link]];
[someString addAttribute:NSFontAttributeName value:linkFont range:NSMakeRange(24, link.length)];
UITextView *textView = [[UITextView alloc] init];
textView.attributedText = someString;
...

Format text in UITextView

I am making an "About Page" for my application, and I have to paste pretty huge amount of text on that page (text will not be editable).
I am using a UITextView for this purpose. However, I need some words to be in bold (like headings). But I am not able to achieve it. The UITextView removes all my formatting.
I have no code to show here because I am pasting all my text in the IB only.
Is there any workaround?? For example.. I want the heading in bold..
"About ABC Corporation
gkskgsagdfgfskgfjkgfgljdgsfjgdsjfgdjlsgflgdslgfljdsgfljgdsjlgfljdsgfljdgslfgls.
jdfjkhdsjlfhdlsfhkldshfkldshflkdhlkfhdklsfhlksdhflkdshflkhdsflkhsdklfhlksdhflkshf
fjhdshfkldshfkldhsfklhdsklfhlkdshfklhdsklfhdklsfhkdshfklhdsklfhklsdfhkldshfkhdsklf
fhjdgfkdgsjkfgjkdsgfjkdsgjfgjdkgfjgsjdfgjkdsgfjkgsdjkfgjsdgfjgsdjkfgjksdgfjkgskjfgs"
First keep text property of textView as 'Attributed', after that just select the text and change it's style and font as you want.
P.S. - All you have to do this in Attribute inspector panel of IB.
EDIT:
For more, visit this link .
Also, this is very good mainly when the textField have a text that will not change, e.g. - like About info. And when this text is inserted in IB directly instead of assigning text programmaticaly.
You can use NSAttributedString, Set Text Font, Foreground And Background Colors, StrikeThrough And Shadow etc..
Attributed strings make an association between characters and their attributes. Like NSString objects, there are two variations, NSAttributedString and NSMutableAttributedString. Although previous versions of iOS supported attributed strings, it wasn’t until iOS 6 that controls such as buttons, labels, textfields and textviews defined a property to manage attributes. Attributes are applied to a range of characters, so you can for example, set a strikethrough attribute for just a portion of a string. It’s also important to note that the default font for attributed string objects is Helvetica 12-point. Keep this in mind if you set the font attribute for a range other than the complete string. The following attributes can be set with attributed strings:
NSString *const NSFontAttributeName;
NSString *const NSParagraphStyleAttributeName;
NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;
NSString *const NSLigatureAttributeName;
NSString *const NSKernAttributeName;
NSString *const NSStrikethroughStyleAttributeName;
NSString *const NSUnderlineStyleAttributeName;
NSString *const NSStrokeColorAttributeName;
NSString *const NSStrokeWidthAttributeName;
NSString *const NSShadowAttributeName;
NSString *const NSVerticalGlyphFormAttributeName;
// Create attributed string
NSString *str = #"example for underline \nexample for font \nexample for bold \nexample for italics";
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:str];
// Add attribute NSUnderlineStyleAttributeName
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:NSMakeRange(12, 9)];
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
range:NSMakeRange(12, 9)];
// Set background color for entire range
[attributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor yellowColor]
range:NSMakeRange(0, [attributedString length])];
// Create NSMutableParagraphStyle object
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
// Add attribute NSParagraphStyleAttributeName
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraph
range:NSMakeRange(0, [attributedString length])];
// Set font, notice the range is for the whole string
UIFont *font = [UIFont fontWithName:#"Helvetica" size:18];
[attributedString addAttribute:NSFontAttributeName
value:font
range:NSMakeRange(35, 4)];
// Set font, notice the range is for the whole string
UIFont *fontBold = [UIFont fontWithName:#"Helvetica-Bold" size:18];
[attributedString addAttribute:NSFontAttributeName
value:fontBold
range:NSMakeRange(53, 4)];
// Set font, notice the range is for the whole string
UIFont *fontItalics = [UIFont fontWithName:#"Helvetica-Oblique" size:18];
[attributedString addAttribute:NSFontAttributeName
value:fontItalics
range:NSMakeRange(71, 7)];
// Set label text to attributed string
[self.mytextView setAttributedText:attributedString];
Check out attributedText property of UITextView
https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView/attributedText
And this how you can make part of your string bold
Any way to bold part of a NSString?
You can use NSMutableAttributedString to support multi attribute property to your string.Note:- NSMutableAttributedString is available in iOS 6.0+. Edit:- Check this:- How to create a UILabel or UITextView with bold and normal text in it?
iOS SDK has included text kit in it.
iOS 7 offers the ability to enhance the legibility of text by increasing font weight, as well as an option to set the preferred font size for apps that support dynamic text. Users will expect apps written for iOS7 to honor these settings, so ignore them at your own risk!
In order to make use of dynamic type you need to specify fonts using styles rather than explicitly stating the font name and size. With iOS 7 a new method has been added to UIFont, preferredFontForTextStyle that creates a font for the given style using the user’s font preferences.
Here is a great tutorial in great website Ray wenderlich text kit tutorial
If you have time, watch WWDC 2013 video session 201
UITextView is a plain text and you can't apply different styles(font,color) for different words.
I suggest you to use UIWebView. It is html, so it can be styled.

Resources