gpt4 book ai didi

ios - UITextView 自定义

转载 作者:可可西里 更新时间:2023-11-01 06:18:47 24 4
gpt4 key购买 nike

我想在我的 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>

最佳答案

您可以使用 NSAttributedString,设置文本字体、前景色和背景色、删除线和阴影等。

属性字符串在字符和它们的属性之间建立关联。与 NSString 对象一样,有两种变体,NSAttributedString 和 NSMutableAttributedString。虽然之前的 iOS 版本支持属性字符串,但直到 iOS 6 才对按钮、标签、文本字段和 TextView 等控件定义了一个属性来管理属性。属性应用于一定范围的字符,因此您可以为字符串的一部分设置删除线属性。同样重要的是要注意,属性字符串对象的默认字体是 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];

`

关于ios - UITextView 自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14748225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com