1

I added a .txt file to bundle and this file consists of like five paragraphs and each paragraphs has a title. I successfully load that .txt file to a string variable and display the text in my UITextView. But i want like each title have different font and each paragraph have different font size. Please help me.

I am using the following code:

NSString *path = [[NSBundle mainBundle]pathForResource:@"License" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.textView setText:content];
Larme
  • 24,190
  • 6
  • 51
  • 81
IKKA
  • 6,297
  • 7
  • 50
  • 88

3 Answers3

0

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];

For more info Refer here

Community
  • 1
  • 1
PK20
  • 1,066
  • 8
  • 19
0

This may help

self.textView.attributedText =
[   NSAttributedString.alloc
    initWithFileURL:[ NSBundle.mainBundle URLForResource:@"License" withExtension:@"txt"  ]
    options:nil
    documentAttributes:nil
    error:err];

Edit : if each individual words or characters or paragraph needs to be format then its better to use html code and load it with NSAttributedString. Thanks

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
0

My extension function to convert html to NSAttributedString. If the License.txt is composed by html syntax, then it could convert to NSAttributedString directly.

@implementation NSString (Html)
- (NSAttributedString *)htmlToAttributeString
{
    NSAttributedString *str = [[NSAttributedString alloc] initWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
                                                               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                         NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                    documentAttributes:nil
                                                                 error:nil];
    return str;
}
@end
AechoLiu
  • 17,522
  • 9
  • 100
  • 118