用TextKit實現表情,圖片,文字混排,textkit表情

來源:互聯網
上載者:User

用TextKit實現表情,圖片,文字混排,textkit表情

用Xcode以前本身內建的coreText,coreImage,實現圖文混排,代碼量非常大,不容易理解,

而Textkit是iOS7新推出的類庫,其實是在之前推出的CoreText上的封裝, TextKit並沒有新增的類,他是在原有的文本顯示控制項上的封裝,可以使用平時我們最喜歡使用的 UILabel,UITextField,UITextView裡面就可以使用了。

 

   //傳你需要處理的常值內容

    NSString * str = @"nni-hao-textkit";

 

    //建立NSMutableAttributedString

    // 這是所有TextKit的載體,所有的資訊都會輸入到NSAttributedString裡面,然後將這個String輸入到Text控制項裡面就可以顯示了。    

    NSMutableAttributedString * attributed=[[NSMutableAttributedString alloc] initWithString:str];

    //給所有字元設定字型為

    //給所有字元設定字型為Zapfino,字型為30

    [attributed addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 30]

                            range: NSMakeRange(0, str.length)];

    //分段控制,最開始4個字元顏色設定成黃色

    [attributed addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange(0, 4)];

    //分段控制,第5個字元開始的3個字元,即第5、6、7字元設定為紅色

    [attributed addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];

    //建立UITextView來顯示

    UITextView * lable = [[UITextView alloc] initWithFrame:self.view.bounds];

    lable.textAlignment = UITextAlignmentCenter;

    lable.attributedText = attributed;

    [self.view addSubview:lable];

   //控制台顯示

                   

   //*********************************************************************************    

    //建立圖片附件

    NSTextAttachment *attach = [[NSTextAttachment alloc] init];

 

    attach.image = [UIImage imageNamed:@"3.jpg"];

    attach.bounds = CGRectMake(0, 70, 50, 50);

    

    NSAttributedString * attachstring = [NSAttributedString attributedStringWithAttachment:attach];

    

    [attributed appendAttributedString:attachstring];

   //控制台顯示:

                             

   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.