ios 在 UITextView 中格式化文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20781506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format text in UITextView
提问by Shradha
I am making an "About Page" for my application, and I have to paste pretty huge amount of text on that page (text will not be editable).
我正在为我的应用程序制作一个“关于页面”,我必须在该页面上粘贴大量文本(文本不可编辑)。
I am using a UITextView
for this purpose. However, I need some words to be in bold (like headings). But I am not able to achieve it. The UITextView
removes all my formatting.
我正在UITextView
为此目的使用 a 。但是,我需要一些粗体字(如标题)。但我无法实现它。在UITextView
移除了我所有的格式。
I have no code to show here because I am pasting all my text in the IB only.
我没有代码可在此处显示,因为我仅将所有文本粘贴到 IB 中。
Is there any workaround?? For example.. I want the heading in bold..
有什么解决办法吗??例如..我想要粗体标题..
"About ABC Corporation
"关于 ABC 公司
gkskgsagdfgfskgfjkgfgljdgsfjgdsjfgdjlsgflgdslgfljdsgfljgdsjlgfljdsgfljdgslfgls.
jdfjkhdsjlfhdlsfhkldshfkldshflkdhlkfhdklsfhlksdhflkdshflkhdsflkhsdklfhlksdhflkshf
fjhdshfkldshfkldhsfklhdsklfhlkdshfklhdsklfhdklsfhkdshfklhdsklfhklsdfhkldshfkhdsklf
fhjdgfkdgsjkfgjkdsgfjkdsgjfgjdkgfjgsjdfgjkdsgfjkgsdjkfgjsdgfjgsdjkfgjksdgfjkgskjfgs"
采纳答案by Nayan
First keep text property of textView
as 'Attributed
', after that just select the text and change it's style and font as you want.
首先将 text 属性保留textView
为 ' Attributed
',然后只需选择文本并根据需要更改其样式和字体。
P.S.- All you have to do this in Attribute inspector panel of IB.
PS- 您必须在 IB 的属性检查器面板中执行此操作。
EDIT:
编辑:
For more, visit this link.
有关更多信息,请访问此链接。
Also, this is very good mainly when the textField
have a text that will not change, e.g. - like About info. And when this text is inserted in IB directly instead of assigning text programmaticaly.
此外,这非常好,主要是当textField
文本不会改变时,例如 - 像关于信息。并且当此文本直接插入 IB 而不是以编程方式分配文本时。
回答by Arun Kumar
You can use NSAttributedString
, Set Text Font, Foreground And Background Colors, StrikeThrough
And Shadow etc..
您可以使用NSAttributedString
、设置文本字体、前景色和背景色StrikeThrough
以及阴影等。
Attributed strings make an association between characters and their attributes. Like NSString
objects, there are two variations, NSAttributedString
and NSMutableAttributedString
. Although previous versions of iOS
supported attributed strings, it wasn't until iOS 6
that controls such as buttons, labels, textfields and textviews defined a property to manage attributes. Attributes are applied to a range of characters, so you can for example, set a strikethrough
attribute for just a portion of a string. It's also important to note that the default font for attributed string objects is Helvetica 12-point. Keep this in mind if you set the font attribute for a range other than the complete string. The following attributes can be set with attributed strings:
属性字符串在字符及其属性之间建立关联。像NSString
对象一样,有两种变体,NSAttributedString
和NSMutableAttributedString
。虽然以前版本iOS
支持属性字符串,但直到iOS 6
按钮、标签、文本字段和文本视图等控件定义了一个属性来管理属性。属性应用于一系列字符,例如,您可以strikethrough
仅为字符串的一部分设置属性。同样重要的是要注意属性字符串对象的默认字体是 Helvetica 12-point。如果您为完整字符串以外的范围设置字体属性,请记住这一点。可以使用属性字符串设置以下属性:
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];
回答by Piyush Dubey
You can use NSMutableAttributedString
to support multi attribute property to your string.
Note:- NSMutableAttributedString
is available in iOS 6.0+.
Edit:-
Check this:- How to create a UILabel or UITextView with bold and normal text in it?
您可以使用NSMutableAttributedString
支持字符串的多属性属性。
注意:-NSMutableAttributedString
在 iOS 6.0+ 中可用。
编辑:-
检查这个:-如何创建一个带有粗体和普通文本的 UILabel 或 UITextView?
回答by Ossir
Check out attributedText
property of UITextView
https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView/attributedText
查看https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView/attributedText 的attributedText
属性UITextView
And this how you can make part of your string bold Any way to bold part of a NSString?
这如何使您的字符串的一部分加粗 任何方式来加粗 NSString 的一部分?
回答by Prince Agrawal
iOS SDK has included text kit in it. iOS 7 offers the ability to enhance the legibility of text by increasing font weight, as well as an option to set the preferred font size for apps that support dynamic text. Users will expect apps written for iOS7 to honor these settings, so ignore them at your own risk! In order to make use of dynamic type you need to specify fonts using styles rather than explicitly stating the font name and size. With iOS 7 a new method has been added to UIFont, preferredFontForTextStyle that creates a font for the given style using the user's font preferences.
iOS SDK 已包含文本工具包。iOS 7 提供了通过增加字体粗细来增强文本易读性的能力,以及为支持动态文本的应用程序设置首选字体大小的选项。用户会期望为 iOS7 编写的应用程序遵循这些设置,因此忽略它们,风险自负!为了使用动态类型,您需要使用样式指定字体,而不是明确说明字体名称和大小。在 iOS 7 中,UIFont 中添加了一个新方法 preferredFontForTextStyle,它使用用户的字体首选项为给定的样式创建字体。
Here is a great tutorial in great website Ray wenderlich text kit tutorial
这是很棒的网站上的一个很棒的教程Ray wenderlich 文本工具包教程
If you have time, watch WWDC 2013 video session 201
如果您有时间,请观看 WWDC 2013 视频会议 201