我正在为我的应用程序制作一个“关于页面”,我必须在该页面上粘贴大量文本(文本将无法编辑)。
我正在使用 UITextView
以此目的。但是,我需要一些粗体字(如标题)。但我无法实现它。该 UITextView
删除我的所有格式。
我没有在这里显示的代码,因为我只在IB中粘贴了我的所有文本。
有没有解决办法?例如..我希望标题以粗体显示..
“关于ABC公司
gkskgsagdfgfskgfjkgfgljdgsfjgdsjfgdjlsgflgdslgfljdsgfljgdsjlgfljdsgfljdgslfgls.
jdfjkhdsjlfhdlsfhkldshfkldshflkdhlkfhdklsfhlksdhflkdshflkhdsflkhsdklfhlksdhflkshf
fjhdshfkldshfkldhsfklhdsklfhlkdshfklhdsklfhdklsfhkdshfklhdsklfhklsdfhkldshfkhdsklf
fhjdgfkdgsjkfgjkdsgfjkdsgjfgjdkgfjgsjdfgjkdsgfjkgsdjkfgjsdgfjgsdjkfgjksdgfjkgskjfgs"
首先保留文本属性 textView
作为'Attributed
',之后只需选择文本并根据需要更改其样式和字体。
附: - 您必须在IB的属性检查器面板中执行此操作。
编辑:
更多信息,请访问 这个链接 。
也,这个时候非常好 textField
有一个不会改变的文本,例如 - 关于信息。当这个文本直接插入IB而不是分配文本programmaticaly。
您可以使用 NSAttributedString
,设置文本字体,前景和背景颜色, StrikeThrough
和阴影等..
归因字符串在字符及其属性之间建立关联。喜欢 NSString
对象,有两种变化, NSAttributedString
和 NSMutableAttributedString
。虽然以前的版本 iOS
支持属性字符串,直到 iOS 6
按钮,标签,文本字段和文本视图等控件定义了一个属性来管理属性。属性应用于一系列字符,因此您可以设置一个 strikethrough
仅属于字符串的一部分。同样重要的是要注意,属性字符串对象的默认字体是Helvetica 12-point。如果为整个字符串以外的范围设置font属性,请记住这一点。可以使用属性字符串设置以下属性:
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];
您可以使用 NSMutableAttributedString
支持多属性属性到您的字符串。
注意: - NSMutableAttributedString
适用于iOS 6.0+。
编辑:-
检查一下: - 如何在其中创建带有粗体和普通文本的UILabel或UITextView?
iOS SDK中包含了文本工具包。
iOS 7提供了通过增加字体重量来增强文本易读性的功能,以及为支持动态文本的应用程序设置首选字体大小的选项。用户希望为iOS7编写的应用程序能够遵守这些设置,因此请自行承担风险!
为了使用动态类型,您需要使用样式指定字体,而不是明确说明字体名称和大小。在iOS 7中,UIFont添加了一种新方法preferredFontForTextStyle,它使用用户的字体首选项为给定样式创建字体。
这是伟大的网站上的一个很棒的教程 Ray wenderlich文本工具包教程
如果您有时间,请观看WWDC 2013视频会话201
UITextView是纯文本,您不能为不同的单词应用不同的样式(字体,颜色)。
我建议你使用 UIWebView的。它是html,因此可以设置样式。