Platforms to show: All Mac Windows Linux Cross-Platform

Back to NSAttributedStringMBS class.

NSAttributedStringMBS.boundingRectWithSize(size as NSSizeMBS, Options as Integer = 0) as NSRectMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 24.4 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns the bounding rectangle necessary to draw the string.

size: The width and height constraints to apply when computing the string’s bounding rectangle.
options: Additional drawing options to apply to the string during rendering. For a list of possible values, see NSStringDrawingOptions.

Returns a rectangle whose size component indicates the width and height required to draw the entire contents of the string.

You can use this method to compute the space required to draw the string. The constraints you specify in the size parameter are a guide for the renderer for how to size the string. However, the actual bounding rectangle returned by this method can be larger than the constraints if additional space is needed to render the entire string. Typically, the renderer preserves the width constraint and adjusts the height constraint as needed.
In iOS 7 and later, this method returns fractional sizes (in the size component of the returned rectangle); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
Special Considerations
To calculate the bounding rectangle, this method uses the baseline origin by default, so it behaves as a single line. To render the string in multiple lines, specify NSStringDrawingUsesLineFragmentOrigin in options. (see constants in NSGraphicsMBS class)

NSAttributedStringMBS.Constructor

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 19.0 ✅ Yes ❌ No ❌ No ✅ Yes All
The constructor to create empty attributed string.

NSAttributedStringMBS.Convert_Operator as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 18.4 ✅ Yes ❌ No ❌ No ✅ Yes All
Converts to string by just returning string part.

NSAttributedStringMBS.copy as NSAttributedStringMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a copy of the attributed string.

NSAttributedStringMBS.CopyToClipboard as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 18.0 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Copies styled text to clipboard.

NSAttributedStringMBS.dataFromRange(offset as Integer, length as Integer, documentAttributes as dictionary = nil, byref error as NSErrorMBS) as memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 16.0 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns an data object that contains a text stream corresponding to the characters and attributes within the given range.

offset and length: The range.
documentAttributes: A required dictionary specifying the document attributes. The dictionary contains values from Document Types and must at least contain NSDocumentTypeDocumentAttribute.
error: An in-out variable containing an encountered error, if any.

Returns the data for the attributed string, or nil if failure. When nil, error encapsulates the error information.
Raises an NSRangeException if any part of range lies beyond the end of the receiver’s characters.

NSAttributedStringMBS.docFormatFromRange(documentAttributes as dictionary = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 9.4 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Creates DOC data from the whole string.

Same as docFormatFromRange(0,length)
Returns nil on failure.
documentAttributes can optionally be a dictionary with document attributes like author or title.

See also:

Some examples using this method:

NSAttributedStringMBS.docFormatFromRange(offset as Integer, length as Integer, documentAttributes as dictionary = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Creates DOC data from the current string range.

Returns nil on failure.
documentAttributes can optionally be a dictionary with document attributes like author or title.

See also:

NSAttributedStringMBS.fileWrapperFromRange(offset as Integer, length as Integer, documentAttributes as dictionary = nil, byref Error as NSErrorMBS) as NSFileWrapperMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 16.0 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns an NSFileWrapper object that contains a text stream corresponding to the characters and attributes within the given range.
Example
// get styled text from htmlviewer
Var w as WebViewMBS = HTMLViewer1.WebViewMBS
Var f as WebFrameMBS = w.mainFrame
Var v as WebFrameViewMBS = f.frameView
Var d as WebDocumentViewMBS = v.documentView
Var n as NSAttributedStringMBS = d.attributedString


// package it
Var da as new Dictionary
da.Value(n.NSDocumentTypeDocumentAttribute) = n.NSRTFDTextDocumentType

Var e as NSErrorMBS
Var fw as NSFileWrapperMBS = n.fileWrapperFromRange(0, n.Length, da, e)
if e <> nil then
MsgBox e.LocalizedDescription
Return
end if

// write to disk
Var file as FolderItem = SpecialFolder.Desktop.Child("test.rtfd")
if fw.writeToFile(file, e) then
MsgBox "OK"
else
MsgBox e.LocalizedDescription
end if

offset and length: The range.
documentAttributes: A required dictionary specifying the document attributes. The dictionary contains values from Document Types and must at least contain NSDocumentTypeDocumentAttribute.
error: An in-out variable containing an encountered error, if any.

Returns a file wrapper for the appropriate document type, or nil if failure. When nil, error encapsulates the error information.

Raises an NSRangeException if any part of range lies beyond the end of the receiver’s characters.

NSAttributedStringMBS.GeneratePDF(PrintOptions as Variant = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 16.0 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Creates a PDF for attributed string.
Example
// read file
Var fi as FolderItem = SpecialFolder.Desktop.Child("test.docx")

Var d as new Dictionary
Var n as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithPath(fi, d)

// write pdf
Var p as MemoryBlock = n.GeneratePDF(nil)

Var fo as FolderItem = SpecialFolder.Desktop.Child("test.pdf")
Var bo as BinaryStream = BinaryStream.Create(fo)
bo.Write p

PrintOptions is optional NSPrintInfoMBS object for print settings like margin.

NSAttributedStringMBS.htmlString as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 7.8 ✅ Yes ❌ No ❌ No ✅ Yes All
A self made function to return the text content as html string preserving the style information.
Example
Var s as NSAttributedStringMBS

s=new NSAttributedStringMBS

if s.initwithhtml("<b>Hello</b>") then
MsgBox s.htmlstring // shows "<b>Hello</b>"
MsgBox s.text // shows "Hello"
end if

Does not always work well, but can help.
(Apple has no official function for this)

Some examples using this method:

NSAttributedStringMBS.lineRangeForRange(range as NSRangeMBS) as NSRangeMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 18.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns the range of characters representing the line or lines containing a given range.

Range: A range within the receiver. The value must not exceed the bounds of the receiver.

The range of characters representing the line or lines containing aRange, including the line termination characters.

NSAttributedStringMBS.mutableCopy as NSMutableAttributedStringMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a mutable copy of the attributed string.
Example
// create Hello World in red
Var a as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithString("Hello World")
Var m as NSMutableAttributedStringMBS = a.mutableCopy

m.addAttribute(a.NSForegroundColorAttributeName, NSColorMBS.redColor, new NSRangeMBS(0, m.length))

// put it in a textarea
TextArea1.NSTextViewMBS.textStorage.setAttributedString m

NSAttributedStringMBS.paragraphRangeForRange(range as NSRangeMBS) as NSRangeMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 18.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns the range of characters representing the paragraph or paragraphs containing a given range.

Range: A range within the receiver. The range must not exceed the bounds of the receiver.

The range of characters representing the paragraph or paragraphs containing range, including the paragraph termination characters.

A paragraph is any segment of text delimited by a carriage return (U+000D), newline (U+000A), or paragraph separator (U+2029).

NSAttributedStringMBS.rangeOfTextBlock(textBlock as NSTextBlockMBS, location as Integer) as NSRangeMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 22.0 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the range of the individual text block that contains the specified location.

NSAttributedStringMBS.rangeOfTextList(list as NSTextListMBS, location as Integer) as NSRangeMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 18.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the range of the given text list that contains the given location.

list: The text list.
location: The location in the text list.

Returns the range of the given text list containing the location.

NSAttributedStringMBS.rangeOfTextTable(textTable as NSTextTableMBS, location as Integer) as NSRangeMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 22.0 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the range of the specified text table that contains the specified location.

NSAttributedStringMBS.rtf as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 9.4 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns the content of this attributed string as a RTF string.
Example
Var s as NSAttributedStringMBS

s=new NSAttributedStringMBS

if s.initWithString("Hello") then
MsgBox s.RTF
end if

// shows this text:
// {\rtf1\ansi\ansicpg1252\cocoartf949
// {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
// {\colortbl;\red255\green255\blue255;}
// \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural
//
// \f0\fs24 \cf0 Hello}

Same as RTFFromRange(0,length)

NSAttributedStringMBS.RTFDFileWrapperFromRange(offset as Integer, length as Integer, documentAttributes as dictionary = nil) as NSFileWrapperMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 16.0 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns an NSFileWrapper object that contains an RTFD document corresponding to the characters and attributes within the given range.

offset and length: The range.
documentAttributes: A required dictionary specifying the document attributes. The dictionary contains values from Document Types and must at least contain NSDocumentTypeDocumentAttribute. If there are no document-level attributes, dict can be nil.

Returns a file wrapper containing the RTFD data.

The file wrapper also includes the document-level attributes in docAttributes, as explained in RTF Files and Attributed Strings.
Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.
You can save the file wrapper using the NSFileWrapper method writeToFile.

NSAttributedStringMBS.RTFDFromRange(documentAttributes as dictionary = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 9.4 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates RTFD data from the current string range.

Returns nil on failure.
Same as RTFDFromRange(0,length)
documentAttributes can optionally be a dictionary with document attributes like author or title.

See also:

NSAttributedStringMBS.RTFDFromRange(offset as Integer, length as Integer, documentAttributes as dictionary = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates RTFD data from the current string range.

Returns nil on failure.
documentAttributes can optionally be a dictionary with document attributes like author or title.

See also:

NSAttributedStringMBS.RTFFromRange(documentAttributes as dictionary = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates RTF data from whole string.

Returns nil on failure.
Same as RTFFromRange(0,length)
documentAttributes can optionally be a dictionary with document attributes like author or title.

See also:

NSAttributedStringMBS.RTFFromRange(offset as Integer, length as Integer, documentAttributes as dictionary = nil) as MemoryBlock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates RTF data from the current string range.
Example
// get some attributed text:
Var t as new NSAttributedStringMBS
call t.initWithString("Hello World")

// set document attributes
Var dic as new Dictionary
dic.Value(t.NSBottomMarginDocumentAttribute) = 20
dic.Value(t.NSLeftMarginDocumentAttribute) = 20
dic.Value(t.NSRightMarginDocumentAttribute) = 20
dic.Value(t.NSTopMarginDocumentAttribute) = 20
dic.Value(t.NSAuthorDocumentAttribute) = "Test User"

// get rtf
Var rtf as string = t.RTFFromRange(dic)

// write to file
Var f as FolderItem = SpecialFolder.Desktop.Child("test.rtf")
Var b as BinaryStream = BinaryStream.Create(f, true)
b.Write rtf

Returns nil on failure.
documentAttributes can optionally be a dictionary with document attributes like author or title.

See also:

NSAttributedStringMBS.size as NSSizeMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Text MBS MacBase Plugin 24.4 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns the size necessary to draw the string.

The minimum size required to draw the entire contents of the string.
Discussion
You can use this method prior to drawing to compute how much space is required to draw the string.
This method may return fractional sizes. When setting the size of your view, use the ceil function to round fractional values up to the nearest whole number.

The items on this page are in the following plugins: MBS MacBase Plugin.


The biggest plugin in space...