6

太字で通常のテキストを含むUILabelまたはを作成しようとしています。UITextView

attributedstringを確認しましたが、カスタムセルのラベルにこれを設定すると、テキストが表示されません。

私もこのUITextView setContentToHTMLString:方法を使用しましたが、文書化されておらず、アプリは拒否されます。

誰かがこれに何らかの解決策を与えることができますか?

4

5 に答える 5

5

iOS 6.0までは、通常のUILabelまたはUITextViewでこれを行うことはできませんでしたが、いくつかの可能なオープンソースソリューションでNSAttributedStringオブジェクトを使用できます。

TTAttributedLabelやOHAttributedLabelのように。

iOS SDKに組み込まれているソリューションの1つとして、NSAttributedStringに設定できる文字列プロパティを持つCATextLayerを使用することもできます

そして、以下のコメント投稿者が言うように、はい、あなたは" attributedText"プロパティでこれを行うことができます。ホレイ!(Appleが開発者の頻繁に繰り返される機能要求を聞いている場合)

于 2012-10-08T07:02:38.027 に答える
5

NSAttributedString」を使用して、単一のラベルに複数のフォントテキストを設定CATextLayerし、それをレンダリングするために使用します。

ただ#import "NSAttributedString+Attributes.h"

次に、次のように実装します。

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];
于 2012-10-08T09:59:52.840 に答える
1

これは古いスレッドですが、これは私が自分で発見したものです。少なくともXcodeバージョン4.6.3では、これは属性付きtextViewを使用することで可能です。さらに良いのは、InterfaceBuilderですべてを実行できることです。

手順は次のとおりです。

textViewを目的の場所に配置します。textViewを選択し、[ユーティリティ]パネルの[属性]タブを開きます。textViewのテキストを「属性付き」に変更します。目的のテキストを入力します。次に、太字や下線などを強調表示します。[T]をクリックします。 fontNameの横にある[ボタン]ポップアップで、目的の書体を選択します(例:太字)。[ユーティリティ]パネルに目的の書体が表示されます。

于 2013-07-25T23:32:31.687 に答える
1

次のコードはiOS6.0以降用です。その結果、「これは太字です」というテキストは太字になり、「これは太字ではありません」というテキストになります。通常のテキストになります。

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];
}
于 2015-04-23T15:25:35.990 に答える
0

インスペクターで属性付きテキストを編集する際に問題が発生した場合は、テキストをコピーしてリッチテキストエディターに貼り付け、調整し、textViewテキストオプションを属性付きに切り替えて貼り付けます。ヴワラ。

于 2013-08-23T20:07:58.673 に答える