为什么NSAttributedString的属性现在属于[NSAttributedStringKey:Any]类型但是UITextView的typingAttributes仍然是[String:Any]类型的?

时间:2017-10-19 21:21:24

标签: ios swift uitextview nsattributedstring swift4

我曾经能够生成NSAttributedString并使用UITextViewtypingAttributes设置[String: Any] [String: Any]类型[NSAttributedStringKey: Any] NSAttributedString 1}}。自iOS 11以来,我必须将UITextView属性更改为[String: Any]属性才能生成def if_powerof_3(n): var = n power = 0 while var>1: if var%3 ==0: var=var/3 power=power+1 else: return "{} is not a power of 3".format(n) return "{} is a power of 3".format(n) print (if_powerof_3(27)) ,但import fileinput, csv mark = input("mark:") username = input("username:") with open("marksh.txt", "r+") as f: lines = f.readlines() for line in lines: if username in line: username, markeasy, markmedium , markhard = line.strip().split("\t") with open("marksh.txt", "a")as f: f.write(username + " " + mark + " " + markmedium+ " " + markhard) with open("marksh.txt","r") as f: lines = f.readlines() finduser = input("username:") for line in lines: if finduser in line: username, markeasy, markmedium , markhard = line.strip().split("\t") print((username) + (markeasy)) 仍然属于{{1}}类型。为什么呢?

1 个答案:

答案 0 :(得分:0)

不确定为什么它不是[NSAttributedStringKey: Any]但是你可以像这样转换它

func ConvertToTypingAttributes(_ attr: [NSAttributedStringKey: Any]) -> [String: Any] {
    var result = [String: Any]()
    for (k,v) in attr {
        result[k.rawValue] = v
    }
    return result
}

用法:

var txtView = UITextView()
let p: [NSAttributedStringKey: Any] = [NSAttributedStringKey.backgroundColor: UIColor.green]
txtView.typingAttributes = ConvertToTypingAttributes(p)