14

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"
Nayan
  • 3,014
  • 2
  • 17
  • 33
Shradha
  • 991
  • 3
  • 13
  • 38

6 Answers6

11

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.

Nayan
  • 3,014
  • 2
  • 17
  • 33
5

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];
ArVID220u
  • 374
  • 3
  • 10
Arun Kumar
  • 154
  • 10
1

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?

Community
  • 1
  • 1
Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
1

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?

Community
  • 1
  • 1
Ossir
  • 3,109
  • 1
  • 34
  • 52
0

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

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
-7

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.