2012-10-08 56 views
6

我正在嘗試創建一個UILabelUITextView,其中包含粗體和正常文本。如何使用粗體和普通文本創建UILabel或UITextView?

我已經通過了歸檔字符串,但是當我在自定義單元格的標籤中設置它時,它不顯示任何文本。

我也用了UITextView setContentToHTMLString:方法,但它是無證和應用程序被拒絕。

任何人都可以提供某種解決方案嗎?

+0

哇......我從來沒有見過一個問題成爲一個「社區維基」(在幾分鐘內的9個修訂版)如此之快! –

+1

我仍然不明白爲什麼這個問題轉換爲社區維基。 – Krishnabhadra

回答

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

直到iOS的6.0,你不能用正常的UILabel或UITextView中做到這一點,但您可以使用NSAttributedString有幾個可能的開源解決方案的對象。

TTAttributedLabelOHAttributedLabel

iOS SDK內置的一個解決方案,you could also use a CATextLayer which has a string property that can be set to a NSAttributedString

而且,像下面的評論者說,是的,你可以用"attributedText" property做到這一點。 Horray! (蘋果傾聽開發者的經常重複的功能要求)

+0

請注意,UILabel與iOS 6的NSAttributedString兼容。 – Eiko

+0

你實際上可以使用普通的UILabels和UITextView來做到這一點,因爲iOS 6使用'屬性文字'屬性 –

+0

我使用的是iOS 5及以後的版本,它會支持iOS 5嗎? – Rocker

1

我知道這是一個古老的線程,但這是我剛發現自己的東西。至少在Xcode 4.6.3版本中,這可以通過使用屬性textView來實現。更好的是,它可以在Interface Builder中完成!

步驟如下:

將您的TextView在所需位置 選擇的TextView和工具面板下打開屬性選項卡 更改的TextView文本「歸因」 輸入你想要的文字 現在,你要粗體突出顯示任何文本,下劃線等 點擊「T」按鈕旁邊的fontName 在彈出窗口中,選擇所需的字體(例如:黑體) 您應該看到在工具面板中顯示所需的字體 享受!

0

如果你有在檢查編輯屬性文本的問題,複製/粘貼文本到富文本編輯器,對其進行調整,切換TextView的文本選項歸因和粘貼。 Vwala。

1

以下代碼適用於iOS 6.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]; 
} 
相關問題