Customizing UITextView

I want to format the test in my text view, some texts are in bold like this. Is it possible for uitextview? Right now I am using webview with HTML strings. eg:

<html><head><style type=\"text/css\">h3 {color:white;} p {color:pink;} p {text-align: center} p {font-family:helvetica;font-size:20px;}</style></head><body>\
                                         <h3></h3>\
                                         <p><b>some text </b></p>\
                                         <p>Short some text</p>\
                                         <p>Child  Infusion  7.5 to 15 mg/kg/hr<br>ie 7.5 to 15 times weight per hour</p>\
                                         <p>Adult  Infusion  3 to 12 mg/kg/hr<br>ie 3 to 12 mg times weight per hour</p>\
                                         </body></html>

      

+3


source to share


1 answer


You can use NSAttributedString, Set Text Font, Foreground and Background Colors, StrikeThrough And Shadow, etc.

Attribute strings create a relationship between symbols and their attributes. Like NSString objects, there are two options: NSAttributedString and NSMutableAttributedString. While previous versions of iOS supported attributed strings, it wasn't until iOS 6, which controls such buttons, labels, text boxes, and text boxes, defined a property to manage attributes. Attributes apply to a range of characters, so you can, for example, set the strikethrough attribute on only a portion of a string. It is also important to note that the default font for attributed string objects is the 12 point version of Helvetica. Keep this in mind if you set the font attribute for a range other than a full line. The following attributes can be set with attributes: 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;NSString * const NSStrokeWidthAttributeName; NSString * const NSShadowAttributeName; NSString * const NSVerticalGlyphFormAttributeName;NSString * const NSStrokeWidthAttributeName; NSString * const NSShadowAttributeName; NSString * const NSVerticalGlyphFormAttributeName;

here are some examples



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

      

`

+16


source







All Articles