这次研究的目标是NSTextView 控件的文本处理图片处理问题
这是一个初步演示,在这里我们建立一个类来处理文本的写入问题。你建立一个Cocoa工程,然后在View里拖入一个NSTextView对象,并在
ViewController类中添加 textView 变量。然后在viewDidLoad() 函数中添加代码。令人你需要找一个girl.jpg文件,也拖入工程。
用到的代码全部列在后面,就不解释了。
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let attachmentCell = NSTextAttachmentCell(imageCell: NSImage(named: "girl.jpg")) let attachment = NSTextAttachment() attachment.attachmentCell = attachmentCell let attributedStr = NSAttributedString(attachment: attachment) self.textView.textStorage?.insert(attributedStr,at: 0) let designer = TextAttributedDesigner("Designer Example") designer.newline() designer.append("ForegroundColor",attrType: .foregroundColor(NSColor.blue)) designer.newline() designer.append("BackgroundColor",attrType: .backgroundColor(NSColor.lightGray)) designer.newline() designer.append("Font (Cochin-BoldItalic)",attrType: .font(NSFont(name: "Cochin-BoldItalic",size: 30)!)) designer.newline() let para = NSMutableParagraphStyle() para.alignment = .center designer.append("this is paragraph text,the text is so length over than normal text line.",attrType: AttributeItemType.paragraphStyle(para)) designer.newline() let para1 = NSMutableParagraphStyle() para1.firstLineHeadindent = 20 designer.append("this is left indent text,the text is so length than normal text line.this is left indent text,the text is so length over than normal text line.this is backgroundColor indent text,attrType: AttributeItemType.paragraphStyle(para1)) designer.newline() designer.append("ligature",attrType: .ligature(0)) designer.newline() designer.append("the kern is long.",attrType: .kern(4)) designer.newline() designer.append("strikethrough",attrType: .strikethrough(4)) designer.newline() designer.append("strikethrough",attrType: .strikethrough(3)) designer.newline() designer.append("strikethrough",attrType: .strikethrough(2)) designer.newline() designer.append("strikethrough",attrType: .strikethrough(1)) designer.newline() designer.append("strikethrough",attrType: .strikethrough(0)) designer.newline() designer.append("strikethrough",attrType: .strikethrough(9)) designer.newline() designer.append("underline",attrType: .underline(5)) designer.newline() designer.append("underline",attrType: .underline(3)) designer.newline() designer.append("underline",attrType: .underline(1)) designer.newline() designer.append("underline",attrType: .underline(0)) designer.newline() designer.append("strokeColor",attrType: .strokeColor(NSColor.blue)) designer.newline() let shdw = NSShadow() shdw.shadowColor = NSColor.brown shdw.shadowBlurRadius = 3 shdw.shadowOffset = NSMakeSize(3,-3) designer.append("Shadow",attrType: .shadow(shdw)) designer.newline() designer.append("TextEffect",attrType: .textEffect(NSTextEffectLetterpressstyle as Nsstring)) designer.newline() designer.append(" Many Font (Cochin-BoldItalic)",attrTypes: [ .font(NSFont(name: "Cochin-BoldItalic",size: 30)!),.strokeColor(NSColor.blue),.strokeWidth(0.5)]) designer.newline() designer.newline() // 如果想有外框效果。 designer.append("OUTLINE",attrTypes: [.strokeColor(NSColor.blue),.strokeWidth(1) ]) designer.newline() self.textView.insertText( designer.attributedString,replacementRange: NSMakeRange(0,0)) }
// // TextAttributedDesigner.swift // TestTextKit // // Created by wuzhiqiang on 2016/10/11. // copyright © 2016年 wuzhiqiang. All rights reserved. // import Cocoa public enum AttributeItemType { case font(NSFont) case paragraphStyle(NSParagraphStyle) case foregroundColor(NSColor) case backgroundColor(NSColor) case ligature(NSNumber) // 是否使用连字符 case kern(NSNumber) // 字距调整 case strikethrough(NSNumber) case underline(NSNumber) case strokeColor(NSColor) case strokeWidth(NSNumber) case shadow(NSShadow) case textEffect(Nsstring) case attachment(NSTextAttachment) case link(NSURL) case baselineOffset(NSNumber) case obliqueness(NSNumber) case expansion(NSNumber) case writingDirection([NSNumber]) case verticalGlyphForm(NSNumber) case markedClauseSegment(NSNumber) case textAlternatives(NSTextAlternatives) case spellingState(NSNumber) case cursor(NSNumber) case toolTip(Nsstring?) public var key: String { switch self { case .font: return NSFontAttributeName case .paragraphStyle: return NSParagraphStyleAttributeName case .foregroundColor: return NSForegroundColorAttributeName case .backgroundColor: return NSBackgroundColorAttributeName case .ligature: return NSligatureAttributeName case .kern: return NSKernAttributeName case .strikethrough: return NsstrikethroughStyleAttributeName case .underline: return NSUnderlinestyleAttributeName case .strokeColor: return NSstrokeColorAttributeName case .strokeWidth: return NSstrokeWidthAttributeName case .shadow: return NSShadowAttributeName case .textEffect: return NSTextEffectAttributeName case .attachment: return NSAttachmentAttributeName case .link: return NSLinkAttributeName case .baselineOffset: return NSBaselineOffsetAttributeName case .obliqueness: return NSObliquenessAttributeName case .expansion: return NSExpansionAttributeName case .writingDirection: return NSWritingDirectionAttributeName case .verticalGlyphForm: return NsverticalGlyphFormAttributeName case .markedClauseSegment: return NSMarkedClauseSegmentAttributeName case .textAlternatives: return NSTextAlternativesAttributeName case .spellingState: return NsspellingStateAttributeName case .cursor: return NSCursorAttributeName case .toolTip: return NSToolTipAttributeName } } public var keyvaluePair: [String: Any] { switch self { case .font(let value): return [self.key : value] case .paragraphStyle(let value): return [self.key : value] case .foregroundColor(let value): return [self.key : value] case .backgroundColor(let value): return [self.key : value] case .ligature(let value): return [self.key : value] case .kern(let value): return [self.key : value] case .strikethrough(let value): return [self.key : value] case .underline(let value): return [self.key : value] case .strokeColor(let value): return [self.key : value] case .strokeWidth(let value): return [self.key : value] case .shadow(let value): return [self.key : value] case .textEffect(let value): return [self.key : value] case .attachment(let value): return [self.key : value] case .link(let value): return [self.key : value] case .baselineOffset(let value): return [self.key : value] case .obliqueness(let value): return [self.key : value] case .expansion(let value): return [self.key : value] case .writingDirection(let value): return [self.key : value] case .verticalGlyphForm(let value): return [self.key : value] case .markedClauseSegment(let value): return [self.key : value] case .textAlternatives(let value): return [self.key : value] case .spellingState(let value): return [self.key : value] case .cursor(let value): return [self.key : value] case .toolTip(let value): return [self.key : value] } } } public struct TextAttributedDesigner { internal let attrText = NSMutableAttributedString() public var attributedString: NSAttributedString { return attrText } // Texkit Support public init(_ text: String,attrType: AttributeItemType? = nil ){ self.append(text,attrType: attrType ) } public func newline(attrType: AttributeItemType? = nil){ self.append( "\r\n",attrType: attrType ) } public func append(_ text: String,attrType: AttributeItemType? = nil ){ let attr = NSAttributedString(string: text,attributes: attrType?.keyvaluePair ) self.attrText.append( attr ) } public func insert(_ text: String,at loc: Int,attributes: attrType?.keyvaluePair ) self.attrText.insert( attr,at: loc) } public func append(_ text: String,attrTypes: [AttributeItemType] ){ let attr = NSAttributedString(string: text,attributes: ItemTypestoDict(attrTypes: attrTypes) ) self.attrText.append( attr ) } public func insert(_ text: String,attributes: ItemTypestoDict(attrTypes: attrTypes) ) self.attrText.insert( attr,at: loc) } ////////// private func ItemTypestoDict(attrTypes: [AttributeItemType] )->[String: Any] { var dict = [String:Any]() for attr in attrTypes { let keyvalue = attr.keyvaluePair for (key,value) in keyvalue { dict[key] = value } } return dict } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。