Format text in UITextView

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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.

I have no code to show here because I am pasting all my text in the IB only.

Is there any workaround?? For example.. I want the heading in bold..

"About ABC Corporation

gkskgsagdfgfskgfjkgfgljdgsfjgdsjfgdjlsgflgdslgfljdsgfljgdsjlgfljdsgfljdgslfgls. jdfjkhdsjlfhdlsfhkldshfkldshflkdhlkfhdklsfhlksdhflkdshflkhdsflkhsdklfhlksdhflkshf  fjhdshfkldshfkldhsfklhdsklfhlkdshfklhdsklfhdklsfhkdshfklhdsklfhklsdfhkldshfkhdsklf  fhjdgfkdgsjkfgjkdsgfjkdsgjfgjdkgfjgsjdfgjkdsgfjkgsdjkfgjsdgfjgsdjkfgjksdgfjkgskjfgs"

回答1:

First keep text property of textView as 'Attributed', after that just select the text and change it's style and font as you want.

P.S. - All you have to do this in Attribute inspector panel of 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.



回答2:

You can use NSAttributedString, Set Text Font, Foreground And Background Colors, StrikeThrough And Shadow etc..

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


回答3:

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

And this how you can make part of your string bold Any way to bold part of a NSString?



回答4:

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?



回答5:

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.

Here is a great tutorial in great website Ray wenderlich text kit tutorial

If you have time, watch WWDC 2013 video session 201



回答6:

UITextView is a plain text and you can't apply different styles(font,color) for different words. I suggest you to use UIWebView. It is html, so it can be styled.



转载请标明出处:Format text in UITextView
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!