1. 程式人生 > >iOS中字串和富文字相互轉換

iOS中字串和富文字相互轉換

/**
 *  富文字轉html字串
 */
+ (NSString *)attriToStrWithAttri:(NSAttributedString *)attri{
    NSDictionary *tempDic = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]};
    NSData *htmlData = [attri dataFromRange:NSMakeRange(0, attri.length)
                         documentAttributes:tempDic
                                      error:nil];
    return [[NSString alloc] initWithData:htmlData
                                 encoding:NSUTF8StringEncoding];
}

/**
 *  字串轉富文字
 */
+ (NSAttributedString *)strToAttriWithStr:(NSString *)htmlStr{
    return [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding]
                                            options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                 documentAttributes:nil
                                              error:nil];
}

相關推薦

iOS字串文字相互轉換

/**  *  富文字轉html字串  */ + (NSString *)attriToStrWithAttri:(NSAttributedString *)attri{     NSDictionary *tempDic = @{NSDocumentTypeDocumen

pandas字串日期的相互轉換

原文地址:http://blog.csdn.net/luoganttcc/article/details/77585038?locationNum=8&fps=1 將數字轉化成時間格式 from dateutil.parser import parse a

javastringint的相互轉換

integer chan 也會 tee tex 什麽 產生 chang details java中string和int的相互轉換 int -> String int i=12345;String s="";核心:s=i+""; String -> int s="

Python字串與datetime的相互轉換

1. 字串轉換成datetime物件 from datetime import datetime t = datetime.strptime(append_at, '%Y-%m-%d %H:%M:%S') 結果顯示: <class 'datetime.datetime

Java8 DateLocalDateTime的相互轉換

一.在Java 8中將Date轉換為LocalDateTime 方法1: 將Date轉換為LocalDatetime,我們可以使用以下方法: 1.從日期獲取ZonedDateTime並使用其方法toLocalDateTime()獲取LocalDateTime 2.使用L

利用Google Gson實現JSON字串物件之間相互轉換

最近一個專案需要用到JSON,需要在JSON字串和物件之間相互轉換,在網上找了些資料,發現google的Gson還是比較不錯的。 廢話不說,下面是簡單的例子: 先上原始碼:下載(包含jar包) Person實體類 package com.hsun.json; /** * Pe

JavaStringInt類相互轉換的方法

String Sch_No = "1707020410"; int output = Integer.parseInt(Sch_No); 這樣就將學號“1707020410” 轉換成了純數字1707020410   其他的也是類似的 double d = Double.p

Java String字串Unicode字元相互轉換程式碼(包括混有普通字元的Unicode)

package util; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * <p>Title: String 與 Unicode 互相轉換的工具類</p> * <p>

C#(C sharp)字串時間的相互轉換

C#(C sharp)字串和時間的相互轉換。 一、DateTime –> string 時間型別轉化成字串型別,那是相當的簡單,直接呼叫ToString()方法即可。如: DateTime dt = DateTime.Now; string dtStr = dt.To

XML String字串 document 的相互轉換總結 .

使用dom4j工具類 進行轉換 ===================================== -- XML String字串  轉  XML Document文件 String  

Java8 DateLocalDate的相互轉換

一.簡述 Date物件表示特定的日期和時間,而LocalDate(Java8)物件只包含沒有任何時間資訊的日期。 因此,如果我們只關心日期而不是時間資訊,則可以在Date和LocalDate之間進行轉換。 二.Date轉LocalDate 如果要將j

字串陣列的分割 以及 字串陣列的相互轉換

一、將 $str="Hi I'm dutian"; 以空格分割,並放到陣列中$str = "Hi I'm dutian"; $arr = explode(" ",$str); print_R($arr); 二、將 $arr = array("Hi I'm dutian"); 

前端JS陣列的刪除、新增方式,以及字串陣列的相互轉換、陣列的合併

<script> var array=[1,3,5]; console.log(array); array.push(7,9); console.log("陣列後邊加入元素="+array); /

C語言字串與整數的相互轉換

C語言提供了幾個標準庫函式,可以將任意型別(整型、長整型、浮點型等)的數字轉換為字串,下面列舉了各函式的方法及其說明。 # include <stdlib.h> 將數字轉換為字串   ● itoa():將整型值轉換為字串。   ● ltoa():將長整

C++字串int的相互轉換

int本身也要用一串字元表示,前後沒有雙引號,告訴編譯器把它當作一個數解釋。預設情況下,是當成10進位制(dec)來解釋,如果想用8進位制,16進位制,怎麼辦?加上字首,告訴編譯器按照不同進位制去解釋。8進位制(oct)---字首加0,16進位制(hex)---字首加0

golang stringint型別相互轉換

總結了golang中字串和各種int型別之間的相互轉換方式: string轉成int: int, err := strconv.Atoi(string) string轉成int64: int64

JAVA使用Dom4j實現字串xml檔案相互轉換

在文章《JAVA 使用Dom4j 解析XML》中,介紹了使用Dom44j解析XML,比如節點的解析遍歷、節點的增加、刪除、寫入檔案等操作,本文我們繼續使用dom4j實現xml字串與xml檔案之間的轉

日期字串時間戳相互轉換Java程式碼

最近在做畢業設計的時候,遇到一個需求,拿到一些時間格式,類似於"2012-12-01 12:02:00" 這樣的時間格式,然後讓程式以這些時間為起點,生成類似於"2012-12-01 12:02:01","2012-12-01 12:02:02","2012-12-01 12

javadate時間戳相互轉換以及獲取前一個小時的時間

1、時間戳是指檔案屬性裡的建立、修改、訪問時間。  數字時間戳技術是數字簽名技術一種變種的應用。在電子商務交易檔案中,時間是十分重要的資訊。在書面合同中,檔案簽署的日期和簽名一樣均是十分重要的防止檔案被偽造和篡改的關鍵性內容。數字時間戳服務(DTS:digital t

lua字串時間戳相互轉換

1. 時間戳轉成格式化字串 直接利用函式os.date()將時間戳轉化成格式化字串. ``` local timestamp =