How to create a UILabel or UITextView with bold an

2019-01-26 12:52发布

问题:

I am trying to create a UILabel or UITextView with bold and normal text inside.

I have gone through the attributedstring but when I am setting this in label of my custom cell it doesn't display any text.

I have also used the UITextView setContentToHTMLString: method, but it is undocumented and app get rejected.

Can anyone give some sort of solution to this?

回答1:

Use "NSAttributedString" to set multiple font text in a single label & use CATextLayer to render it:

just #import "NSAttributedString+Attributes.h"

and then implement it like this:

NSString *string1 = @"Hi";

NSString *string2 = @"How are you ?";

NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];

[attr1 setFont:[UIFont systemFontOfSize:20]];

NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]

[attr2 setFont:[UIFont boldSystemFontOfSize:20]];

[attr1 appendAttributedString:attr2]

CATextLayer *textLayer = [CATextLayer layer];

layer.string = attr1;

layer.contentsScale = [[UIScreen mainScreen] scale];

(Your_text_label).layer = textLayer;

OR (if you want to render on a view directly)

[(Your_View_Name).layer addSublayer:textLayer];


回答2:

Up until iOS 6.0, you couldn't do this with a normal UILabel or UITextView, but you can use NSAttributedString objects with a few possible open source solutions.

Like TTAttributedLabel or OHAttributedLabel.

One solution built into the iOS SDK, you could also use a CATextLayer which has a string property that can be set to a NSAttributedString.

And, like the commenters below say, yes you can do this with the "attributedText" property. Horray! (for Apple listening to developer's very often repeated feature requests)



回答3:

I know this is an old thread, but this is something I just discovered myself. At least in Xcode version 4.6.3 this is possible by using an attributed textView. What's even better is that it's possible to all be done in Interface Builder!

Here are the steps:

Place your textView at the desired location Select the textView and open up the Attributes tab under the Utilities panel Change the textView text to "attributed" Enter your desired text Now, highlight whatever text you want bolded, underlined, etc. Click on the "T" button next to the fontName In the popup, select your desired typeface (ex: Bold) You should see the desired typeface displayed in the Utilities panel Enjoy!



回答4:

The following code is for iOS 6.0 and above. The result is that the text "This is bold" will be in bold and "This is not bold." will be normal text.

if ([self.registrationLabel respondsToSelector:@selector(setAttributedText:)])
{
    // iOS6 and above : Use NSAttributedStrings
    const CGFloat fontSize = 17;
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
    UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
    //UIColor *foregroundColor = [UIColor clearColor];

    // Create the attributes
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                              boldFont, NSFontAttributeName, nil];
    NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                              regularFont, NSFontAttributeName, nil];
    const NSRange range = NSMakeRange(0,12); // range of " 2012/10/14 ". Ideally this should not be hardcoded

    // Create the attributed string (text + attributes)
    NSString *text = @"This is bold and this is not bold.;
    NSMutableAttributedString *attributedText =
    [[NSMutableAttributedString alloc] initWithString:text
                                           attributes:subAttrs];
    [attributedText setAttributes:attrs range:range];

    // Set it in our UILabel and we are done!
    [self.registrationLabel setAttributedText:attributedText];
}


回答5:

If you have an issue with editing the attributed text in the inspector, copy/paste the text into a rich text editor, adjust it, switch the textView Text options to Attributed and paste. Vwala.