UITextView 定制化

3

我希望在我的TextView中格式化文本,有些文本需要加粗,有些文本需要斜体。UITextView能够实现这个功能吗?目前我是通过使用带有HTML标签的字符串来使用WebView。

<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>
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接