我正在学习使用Text Kit来管理iOS应用中的文本的教程《使用 Text Kit 管理 iOS 应用中的文本》。原教程是用Objective C编写的,但是我想尝试使用Swift进行操作。
然而,当我遇到以下代码时,我无法弄清如何使用Swift设置UITextView
的标题和其他样式。下面是原始的Objective C代码:
- (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]];
}
我尝试了
textView.setFont =
textView.preferredFontForTextStyle =
不过,这两种方法似乎都没用,并且我在Stack Overflow上也找不到任何Swift的答案。
setFont
和preferredFontForTextStyle
不是UITextView
的方法或属性,所以没有自动完成。我只是猜测。不过我在另一个网站上找到了解决方案。 - Suragch