2015-02-26 62 views
9

Tôi đã làm việc qua hướng dẫn Using Text Kit to Manage Text in Your iOS Apps. Nó được viết cho Objective C nhưng tôi nghĩ tôi sẽ cố gắng làm điều đó bằng cách sử dụng Swift.Làm cách nào để đặt tiêu đề, tiêu đề phụ, nội dung, chú thích và chú thích cho kiểu văn bản động trong Swift?

Tuy nhiên, khi tôi nhận được mã sau, tôi không thể tìm ra cách đặt tiêu đề và các kiểu khác cho UITextView bằng Swift. Đây là mã C Mục tiêu:

- (IBAction)applyHeadlineStyle:(id)sender { 
    [_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]]; 
} 

- (IBAction)applySubHeadlineStyle:(id)sender { 
    [_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]]; 
} 

- (IBAction)applyBodyStyle:(id)sender { 
    [_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]]; 
} 

- (IBAction)applyFootnoteStyle:(id)sender { 
    [_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]]; 
} 

- (IBAction)applyCaption1Style:(id)sender { 
    [_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]]; 
} 

- (IBAction)applyCaption2Style:(id)sender { 
    [_textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]]; 
} 

tôi đã cố gắng

textView.setFont = 
textView.preferredFontForTextStyle = 

Không những có vẻ để làm việc, tuy nhiên, và tôi không thể tìm thấy bất kỳ câu trả lời Swift trên SO.

+0

Xcode tự động hoàn thành nó cho bạn. Bắt đầu viết nó và sau đó nhấn thoát. Hãy thử một lần. –

+0

Vấn đề của tôi là 'setFont' và' preferredFontForTextStyle' không phải là phương thức hay thuộc tính của 'UITextView' nên không có gì để tự động hoàn thành. Tôi đã được chỉ đoán. Tôi đã tìm thấy một giải pháp trên một trang web khác mặc dù. – Suragch

+1

Bạn sẽ nhận thấy rất nhiều phương pháp chỉ không có từ được thiết lập vì nó không phải là cần thiết thử nó luôn luôn mà không cần "thiết lập" –

Trả lời

21

This tutorial có một số mẫu mã chỉ ra cách thực hiện.

// headline 
textView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline) 

// subheadline 
textView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline) 

// body 
textView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body) 

// footnote 
textView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.footnote) 

// caption 1 
textView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.caption1) 

// caption 2 
textView.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.caption2) 

cũng Xem

+0

làm thế nào tôi có thể hoàn tác thiết lập phong cách –

Các vấn đề liên quan