Hướng dẫn nsattributedstring html - html nsattributedstring

Phiên bản Swift: 5.6

Paul Hudson & NBSP; & NBSP; @twostraws & nbsp; & nbsp; Ngày 28 tháng 5 năm 2019

Bạn có thể tạo

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 trực tiếp từ HTML, bao gồm hỗ trợ cho một loạt các định dạng, sử dụng trình khởi tạo đặc biệt và chuyển trong
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
9 cho loại tài liệu của bạn.

Ví dụ: đưa ra HTML sau:

let html = """


Hello, world!

"""

Trước tiên bạn cần chuyển đổi chuỗi đó thành một ví dụ

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];
0, như thế này:

let data = Data(html.utf8)

Bây giờ bạn có thể tạo một

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 từ đó. Đây là một cuộc gọi ném vì bạn có thể cố gắng chuyển đổi một cái gì đó không hợp lệ, vì vậy chúng tôi sẽ sử dụng
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];
2 và bọc nó trong
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];
3:

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}

Nhà tài trợ hack với Swift và tiếp cận cộng đồng Swift lớn nhất thế giới!

Có sẵn từ iOS 7.0

Các giải pháp tương tự

  • Cách chuyển đổi chuỗi tên HTML thành uicolor
  • Cách tải chuỗi HTML vào wkwebview hoặc uiwebview: loadHtmlString ()
  • Cách tạo chuỗi văn bản được định dạng phong phú bằng cách sử dụng nsattributionString
  • Làm thế nào để kết xuất một nsattributingString thành PDF
  • Cách tạo các liên kết có thể khai thác trong nsattributionString

Về cơ sở kiến ​​thức nhanh chóng

Đây là một phần của cơ sở kiến ​​thức Swift, một bộ sưu tập các giải pháp miễn phí, có thể tìm kiếm cho các câu hỏi iOS phổ biến.

Trang này có hữu ích không? Hãy cho chúng tôi biết!

Xếp hạng trung bình: 3,9/5

Tôi đang sử dụng một thể hiện

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];
4 để xử lý một số văn bản và tô màu chính xác, nó cho kết quả là HTML nhưng thay vì hiển thị nó trong
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];
4 Tôi muốn hiển thị nó bằng cách sử dụng
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];
6 với
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8.

Tôi có thể tạo và vẽ

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 nhưng tôi không chắc chắn làm thế nào tôi có thể chuyển đổi và ánh xạ HTML thành chuỗi được quy kết.

Tôi hiểu rằng theo Mac OS X

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 có phương thức
let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
0 nhưng đây là việc bổ sung chỉ có Mac và không có sẵn cho iOS.

Tôi cũng biết rằng có một câu hỏi tương tự với điều này nhưng nó không có câu trả lời, mặc dù tôi sẽ thử lại và xem liệu có ai đã tạo ra một cách để làm điều này và nếu vậy, nếu họ có thể chia sẻ nó.

hỏi ngày 18 tháng 11 năm 2010 lúc 17:38Nov 18, 2010 at 17:38

1

Trong iOS 7, UIKIT đã thêm một phương thức

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
1 có thể khởi tạo
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 bằng HTML, ví dụ: ví dụ:

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];

Trong Swift:

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)

Hướng dẫn nsattributedstring html - html nsattributedstring

Đã trả lời ngày 19 tháng 9 năm 2013 lúc 5:21Sep 19, 2013 at 5:21

pixpixpix

4.9922 Huy hiệu vàng23 Huy hiệu bạc25 Huy hiệu Đồng2 gold badges23 silver badges25 bronze badges

25

Tạo một nsattributionString từ HTML phải được thực hiện trên chủ đề chính!

Cập nhật: Hóa ra việc kết xuất HTML của NSAttributionString phụ thuộc vào WebKit dưới mui xe và phải được chạy trên luồng chính hoặc đôi khi nó sẽ làm hỏng ứng dụng bằng sigtrap.must be run on the main thread or it will occasionally crash the app with a SIGTRAP.

Nhật ký sự cố di tích mới:

Hướng dẫn nsattributedstring html - html nsattributedstring

Dưới đây là phần mở rộng chuỗi Swift 2 an toàn được cập nhật:thread-safe Swift 2 String extension:

extension String {
    func attributedStringFromHTML(completionBlock:NSAttributedString? ->()) {
        guard let data = dataUsingEncoding(NSUTF8StringEncoding) else {
            print("Unable to decode data from html string: \(self)")
            return completionBlock(nil)
        }

        let options = [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                   NSCharacterEncodingDocumentAttribute: NSNumber(unsignedInteger:NSUTF8StringEncoding)]

        dispatch_async(dispatch_get_main_queue()) {
            if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
                completionBlock(attributedString)
            } else {
                print("Unable to create attributed string from html string: \(self)")
                completionBlock(nil)
            }
        }
    }
}

Usage:

let html = "
Here is some HTML
" html.attributedStringFromHTML { attString in self.bodyLabel.attributedText = attString }

Output:

Hướng dẫn nsattributedstring html - html nsattributedstring

Đã trả lời ngày 9 tháng 12 năm 2015 lúc 23:25Dec 9, 2015 at 23:25

Hướng dẫn nsattributedstring html - html nsattributedstring

Andrew Schreiberandrew SchreiberAndrew Schreiber

13.7K6 Huy hiệu vàng45 Huy hiệu bạc53 Huy hiệu Đồng6 gold badges45 silver badges53 bronze badges

4

Tiện ích mở rộng bộ khởi tạo Swift trên NSattributionString

Xu hướng của tôi là thêm điều này như là một phần mở rộng cho

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 thay vì
let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
4. Tôi đã thử nó như một phần mở rộng tĩnh và một bộ khởi tạo. Tôi thích bộ khởi tạo, đó là những gì tôi đã bao gồm dưới đây.

Swift 4

internal convenience init?(html: String) {
    guard let data = html.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
        return nil
    }

    guard let attributedString = try?  NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) else {
        return nil
    }

    self.init(attributedString: attributedString)
}

Swift 3

extension NSAttributedString {

internal convenience init?(html: String) {
    guard let data = html.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
        return nil
    }

    guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
        return nil
    }

    self.init(attributedString: attributedString)
}
}

Thí dụ

let html = "Hello World!"
let attributedString = NSAttributedString(html: html)

Đã trả lời ngày 6 tháng 3 năm 2017 lúc 19:00Mar 6, 2017 at 19:00

Danmobile di động DanMobile Dan

6.1721 Huy hiệu vàng40 Huy hiệu bạc43 Huy hiệu đồng1 gold badge40 silver badges43 bronze badges

4

Đây là tiện ích mở rộng

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
4 được viết bằng Swift để trả về chuỗi HTML là
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8.

let data = Data(html.utf8)
0

Sử dụng,

let data = Data(html.utf8)
1

Ở trên, tôi đã cố tình thêm một unicode \ u2022 để cho thấy rằng nó hiển thị chính xác Unicode.

Một tầm thường: mã hóa mặc định mà

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 sử dụng là
let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
8 (không phải UTF8!).

Đã trả lời ngày 25 tháng 5 năm 2016 lúc 3:23May 25, 2016 at 3:23

Samwizesamwizesamwize

24.3K15 Huy hiệu vàng137 Huy hiệu bạc183 Huy hiệu Đồng15 gold badges137 silver badges183 bronze badges

2

Swift 3.0 Xcode 8 phiên bản

let data = Data(html.utf8)
2

Đã trả lời ngày 28 tháng 9 năm 2016 lúc 18:19Sep 28, 2016 at 18:19

Hướng dẫn nsattributedstring html - html nsattributedstring

fssilvafssilvafssilva

93511 Huy hiệu bạc7 Huy hiệu đồng11 silver badges7 bronze badges

Đã thực hiện một số sửa đổi trên giải pháp của Andrew và cập nhật mã lên Swift 3:

Mã này hiện sử dụng UITextView là

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
9 và có thể kế thừa phông chữ gốc, kích thước phông chữ và màu văn bản của nó

Lưu ý:

extension String {
    func attributedStringFromHTML(completionBlock:NSAttributedString? ->()) {
        guard let data = dataUsingEncoding(NSUTF8StringEncoding) else {
            print("Unable to decode data from html string: \(self)")
            return completionBlock(nil)
        }

        let options = [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                   NSCharacterEncodingDocumentAttribute: NSNumber(unsignedInteger:NSUTF8StringEncoding)]

        dispatch_async(dispatch_get_main_queue()) {
            if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
                completionBlock(attributedString)
            } else {
                print("Unable to create attributed string from html string: \(self)")
                completionBlock(nil)
            }
        }
    }
}
0 là phần mở rộng từ đây

let data = Data(html.utf8)
3

Ví dụ sử dụng:

let data = Data(html.utf8)
4

Đã trả lời ngày 7 tháng 1 năm 2017 lúc 7:52Jan 7, 2017 at 7:52

Anh ấy yifei 何一非 anh ấy yifei 何一非He Yifei 何一非

2.4824 Huy hiệu vàng37 Huy hiệu bạc67 Huy hiệu Đồng4 gold badges37 silver badges67 bronze badges

Swift 4


  • Swift 3
  • Thí dụ
  • Đã trả lời ngày 6 tháng 3 năm 2017 lúc 19:00

let data = Data(html.utf8)
5

Danmobile di động Dan

let data = Data(html.utf8)
6

6.1721 Huy hiệu vàng40 Huy hiệu bạc43 Huy hiệu đồngNov 30, 2017 at 19:28

Đây là tiện ích mở rộng

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
4 được viết bằng Swift để trả về chuỗi HTML là
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8.AamirR

Sử dụng,4 gold badges52 silver badges70 bronze badges

4

Ở trên, tôi đã cố tình thêm một unicode \ u2022 để cho thấy rằng nó hiển thị chính xác Unicode.

Example:

let data = Data(html.utf8)
7

Một tầm thường: mã hóa mặc định mà

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 sử dụng là
let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
8 (không phải UTF8!).

Đã trả lời ngày 25 tháng 5 năm 2016 lúc 3:23Feb 22, 2017 at 19:51

Hướng dẫn nsattributedstring html - html nsattributedstring

0

Samwizesamwize

24.3K15 Huy hiệu vàng137 Huy hiệu bạc183 Huy hiệu ĐồngNov 18, 2010 at 17:42

Swift 3.0 Xcode 8 phiên bảnjer

Đã trả lời ngày 28 tháng 9 năm 2016 lúc 18:195 gold badges45 silver badges69 bronze badges

3

fssilvafssilva:
Try this:

let data = Data(html.utf8)
8

93511 Huy hiệu bạc7 Huy hiệu đồng

let data = Data(html.utf8)
9

Đã thực hiện một số sửa đổi trên giải pháp của Andrew và cập nhật mã lên Swift 3:Feb 26, 2017 at 22:46

Hướng dẫn nsattributedstring html - html nsattributedstring

reza_khalafireza_khalafireza_khalafi

Mã này hiện sử dụng UITextView là

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
9 và có thể kế thừa phông chữ gốc, kích thước phông chữ và màu văn bản của nó6 gold badges52 silver badges81 bronze badges

0

Lưu ý:

extension String {
    func attributedStringFromHTML(completionBlock:NSAttributedString? ->()) {
        guard let data = dataUsingEncoding(NSUTF8StringEncoding) else {
            print("Unable to decode data from html string: \(self)")
            return completionBlock(nil)
        }

        let options = [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                   NSCharacterEncodingDocumentAttribute: NSNumber(unsignedInteger:NSUTF8StringEncoding)]

        dispatch_async(dispatch_get_main_queue()) {
            if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
                completionBlock(attributedString)
            } else {
                print("Unable to create attributed string from html string: \(self)")
                completionBlock(nil)
            }
        }
    }
}
0 là phần mở rộng từ đây

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];

let data = Data(html.utf8)
3

Ví dụ sử dụng:

Hướng dẫn nsattributedstring html - html nsattributedstring

Đã trả lời ngày 7 tháng 1 năm 2017 lúc 7:52

Anh ấy yifei 何一非 anh ấy yifei 何一非114 gold badges1045 silver badges1168 bronze badges

2.4824 Huy hiệu vàng37 Huy hiệu bạc67 Huy hiệu ĐồngDec 30, 2015 at 7:00

Hướng dẫn nsattributedstring html - html nsattributedstring

2

NSAttributionString tiện lợi

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
1

Không có thêm bảo vệApr 6, 2020 at 16:21

ném lỗiStephen Orr

Cách sử dụng2 silver badges6 bronze badges

Đã trả lời ngày 30 tháng 11 năm 2017 lúc 19:28

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
2

AamirraamirrSep 27, 2020 at 11:34

Hướng dẫn nsattributedstring html - html nsattributedstring

Huy hiệu vàng 11K452 Huy hiệu bạc70 Huy hiệu đồngS1LENT WARRIOR

Sử dụng NSHTMLTextDocumentType chậm và khó kiểm soát các kiểu. Tôi khuyên bạn nên thử thư viện của tôi được gọi là Atributika. Nó có trình phân tích cú pháp HTML rất nhanh. Ngoài ra, bạn có thể có bất kỳ tên thẻ và xác định bất kỳ phong cách nào cho họ.4 gold badges42 silver badges59 bronze badges

1

Bạn có thể tìm thấy nó ở đây https://github.com/psharanda/atributika

Lấy cảm hứng từ chủ đề này, một ví dụ OBJC của Pod và Erica Sadun trong cuốn sách nấu ăn của người sành ăn iOS p.80, tôi đã viết một phần mở rộng trên

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
        NSAttributedString.DocumentType.html]
let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                          options: options,
                                                          documentAttributes: nil)
4 và trên
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
8 để qua lại giữa các chuỗi đơn giản HTML và nsattributionStrings và Vice Versa-trên Github tại đây , mà tôi đã tìm thấy hữu ích.

Các chữ ký là (một lần nữa, mã đầy đủ trong một ý chính, liên kết ở trên):signatures are (again, full code in a Gist, link above):

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
3

Đã trả lời ngày 18 tháng 9 năm 2016 lúc 3:57Sep 18, 2016 at 3:57

AmitaibamitaibAmitaiB

1.5661 Huy hiệu vàng18 Huy hiệu bạc18 Huy hiệu đồng1 gold badge18 silver badges18 bronze badges

Tôn vinh gia đình phông chữ, phông chữ năng động Tôi đã pha chế sự ghê tởm này:

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
4

Ngoài ra, bạn có thể sử dụng các phiên bản, điều này được lấy từ và đặt phông chữ trên uilabel sau khi cài đặt quy địnhString

Điều này sẽ làm giảm kích thước và sự táo bạo được gói gọn trong phần phân công mặc dù

Kudos cho đọc qua tất cả các câu trả lời cho đến đây. Bạn là một người đàn ông rất kiên nhẫn hoặc trẻ em.

Đã trả lời ngày 31 tháng 5 năm 2019 lúc 8:57May 31, 2019 at 8:57

Hướng dẫn nsattributedstring html - html nsattributedstring

Anton Tropashkoanton TropashkoAnton Tropashko

5.2304 Huy hiệu vàng38 Huy hiệu bạc63 Huy hiệu Đồng4 gold badges38 silver badges63 bronze badges

Một chức năng để chuyển đổi HTML thành NSAttributionString được quy định sẽ điều chỉnh kích thước động + khả năng truy cập thích ứng cho văn bản.

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
5

Để sử dụng:

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
6

đã trả lời ngày 17 tháng 2 lúc 19:10Feb 17 at 19:10

Hướng dẫn nsattributedstring html - html nsattributedstring

Thêm tiện ích mở rộng này và sau đó sử dụng văn bản. Sau khi sử dụng mã này, chúng tôi có thể sử dụng kích thước tùy chỉnh của văn bản của chúng tôi.

if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
    yourLabel.attributedText = attributedString
}
7

Đã trả lời ngày 29 tháng 9 lúc 9:57Sep 29 at 9:57

Hướng dẫn nsattributedstring html - html nsattributedstring