2013-02-07 75 views
3

我想在我的文本視圖中格式化測試,一些文本以粗體顯示一些斜體樣的東西。 uitextview有可能嗎? 現在我正在使用帶有HTML字符串的webview。 如:UITextView自定義

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

回答

16

您可以使用NSAttributedString,設置文本字體,前景和背景顏色,刪除線和陰影等。

歸因串使字符和它們的屬性之間的關聯。像NSString對象一樣,有兩種變體,NSAttributedString和NSMutableAttributedString。 儘管以前版本的iOS支持屬性字符串,但直到iOS 6時,控件(如按鈕,標籤,文本字段和文本視圖)纔會定義一個屬性來管理屬性。 屬性應用於一系列字符,因此您可以爲例如字符串的一部分設置刪除線屬性。注意屬性字符串對象的默認字體是Helvetica 12點也很重要。如果您爲整個字符串以外的範圍設置字體屬性,請記住這一點。 可以使用屬性字符串設置以下屬性: 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]; 

`

相關問題