Format text in UITextView - ios

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.

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

Bold font style for custom font in iOS application

I have a custom font and it doesn't include bold face. I have no problem using it in web and Android since they use faux bold but, I don't know how can I make it bold in iOS.
I want to know if there is a way to make faux bold in iOS or any tool to create a bold font face from normal one using techniques that browsers use.
You could use NSAttributedString to stroke text and make them bold.
Try this code below:
NSString *string = #"Hello World";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSDictionary *attributes = #{
NSStrokeWidthAttributeName: #-5.0, //value for making bold, higher negative number bolder text
NSStrokeColorAttributeName:[UIColor blackColor],
NSForegroundColorAttributeName:[UIColor blackColor],
NSFontAttributeName:[UIFont fontWithName:#"Your font name" size:20]
};
[attrString addAttributes:attributes range:NSMakeRange(0, string.length)];
Hope this help.

How to show two one label text below other in iOS?

I want to show text of one label below the other label.Although text should start from right of first label & it should go below the other label.Please tell me it is possible using ios ?
I got the solution for your question.It is very easy and understandable.
NSMutableAttributedString *mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:#"User this is just a comment.This just a comment.This just a comment."];
NSAttributedString *attributeStr = [[NSAttributedString alloc]initWithString:#"\n" attributes:#{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-Bold" size:8]}];
[mutableAttributeStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica-Bold" size:14.0] range:NSMakeRange(0, 4)];
[mutableAttributeStr appendAttributedString:attributeStr];
labelTextBelowAnotherLabel.numberOfLines = 0;
[labelTextBelowAnotherLabel setAttributedText:mutableAttributeStr];
Above in coding i set the helvetica bold and fontsize for reference.If you want to change to systemBold just change and also set the font size according to your requirements.
Thank You-:)
At run time display you use NSMutableAttributedString.
e.g.
NSDictionary *dicAttributes = #{NSForegroundColorAttributeName:WHITE_COLOR, NSFontAttributeName: UI_DEFAULT_ROBOTO_BOLD(15.0)};
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:#"here is your text which want to set" attributes:dicAttributes];
Yes this is possible in One label with storyboard.Just use one label and set text as a Attributed.
and select User word and just right click and you find option named as a "Font". now you can select as a Bold,Italic,UnderLine etc.
This is easy way to display text in one Label.
It may be helpful to you.

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

Resources