有人可以帮我用Swift 2将rtf文本加载到UITextView中吗?我得到的答案已经过时了。该文本是关于如何玩我在应用程序中编写的游戏的说明。到目前为止,我所能做的就是将所有rtf文本复制并粘贴到占位符框中。这适用于模拟器中的iPhone,但是当我在iPad模拟器或iPhone 6 Plus中尝试时,出现这种情况时会出现双垂直滚动条。它看起来很乱。
我现在也有一个相同文件的真实纯文本,所以我们也可以试试。
答案 0 :(得分:24)
Swift 3
def function_1():
yield "start_function_2"
counter = 0
while True:
counter += 1
print counter
def function_2():
second_counter = 0
while True:
second_counter += 1
print second_counter
def main():
return render_template("index.html")
@app.route("/start_functions", methods=["POST"])
def start_functions():
data = request.data
if request.method == "POST":
for i in function_1(data):
if (i == "start_function_2"):
function_2()
if __name__ == "__main__":
app.run(dhost="0.0.0.0", port=port)
Swift 4& 5 强>
Fetching origin...
Pruning stale remotes...
Getting remote branches...
Checking branch master
Does not meet criteria
Done.
Finished: SUCCESS
答案 1 :(得分:1)
if let rtfPath = NSBundle.mainBundle().URLForResource("description_ar", withExtension: "rtf")
{
let attributedStringWithRtf = NSAttributedString(fileURL: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)
self.textView.attributedText = attributedStringWithRtf
}
答案 2 :(得分:1)
您可以在Swift 2中使用以下代码阅读rtf文件。
加载RTF文件
let path = NSBundle.mainBundle().pathForResource("sample-rtf", ofType: "rtf")
let contents: NSString
do {
contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
} catch _ {
contents = ""
}
let array : NSArray = contents.componentsSeparatedByString("\n");
self.textView.text = (array.objectAtIndex(0) as! String);
//self.textView.text = contents as String
尝试使用纯文本(txt)文件而不是rtf。 RTF文件也包含有关文本的格式信息。这是你在阅读内容后看到的不必要的东西。
在Mac TextEdit中打开rtf文件,然后按Cmd + Shift + T(这会将其转换为纯文本并删除所有格式),然后另存为txt。
加载文字文件
let path = NSBundle.mainBundle().pathForResource("sample-text", ofType: "txt")
let contents: NSString
do {
contents = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
} catch _ {
contents = ""
}
self.textView.text = contents as String
答案 3 :(得分:1)
Swift 3代码:
if let rtfPath = Bundle.main.url(forResource: "description_ar", withExtension: "rtf") {
do {
let attributedStringWithRtf:NSAttributedString = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
self.textView.attributedText = attributedStringWithRtf
} catch {
print("We got an error \(error)") //or handle how you want
}
}
@MikeG启发的编辑和更新 10月21日更新的灵感来自@ RAJAMOHAN-S和@biomiker