NSTextView 研究1

这次研究的目标是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
    }

}


这里的代码简化了NSTextView的文本和图片的插入。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...